Search code examples
rshinyshiny-servershinydashboard

Shiny Reactive Plot Fail


I have a series(100+) of plots that I'd like to display depending on what crime & year the user selects (reactive)

The plots are named already, in this format: "CrimeDataA01" for Assaults in 2001, "CrimeDataM02" for Murder in 2002, etc.

The plots have been defined and the code for them is pasted before shinyServer(function(input, output) in the server file.

I'm trying to get Shiny to print the correct plot, depending on what crime and year the user selects. Here is my server.r code:

shinyServer(function(input, output) {
crime2 <- switch(input$select,
               "Assault" = "A",
               "Burglary" = "B",
               "Car Theft" = "MT",
               "Grand Larceny" = "G",
               "Murder" = "M",
               "Rape" = "R",
               "Robbery" = "RO")
year2 <- switch(input$Slider,
               "2000" = "00",
               "2001" = "01",
               "2002" = "02",
               "2003" = "03",
               "2004" = "04",
               "2005" = "05",
               "2006" = "06",
               "2007" = "07",
               "2008" = "08",
               "2009" = "09",
               "2010" = "10",
               "2011" = "11",
               "2012" = "12",
               "2013" = "13",
               "2014" = "14",
               "2015" = "15")
output$plot <- plot(paste0("CrimeData", crime2, year2))
  })
})

Here is my ui.R code:

library(shiny)
shinyUI(fluidPage(
  sidebarLayout( position = "right",
sidebarPanel(
      selectInput("select", label = h3("Select Crime"),
              choices = list("Assault" = 1, "Burglary" = 2, "Car Theft" = 3, "Grand Larceny" = 4, "Murder" = 5, "Rape" = 6, "Robbery" = 7))
),
    mainPanel(
  textOutput("text1"),
  h1("NYC Crime Over Time", align = "center"),
  sliderInput("Slider", "Year", 2000, 2015, 2000),
print(plot)
  ))))

I've messed with it so much I don't know what is up or down anymore. I'd just like it to call the correct plot and display it


Solution

  • In case I got the question right you are looking for the following code: Note that I only defined CrimeDataA00 and CrimeDataA01, the other plots you seem to have saved locally. In addition to Brandon`s suggestion to look into renderImage() or renderPlot() you should get familiar with reactives() and get(). Good luck!

    plot(1:9)
    CrimeDataA00 <- recordPlot()
    
    server <- shinyServer(function(input, output) {
      crime2 <- reactive({
        switch(input$select,
               "Assault" = "A",
               "Burglary" = "B",
               "Car Theft" = "MT",
               "Grand Larceny" = "G",
               "Murder" = "M",
               "Rape" = "R",
               "Robbery" = "RO")  
      })
    
      year2 <- reactive({
        switch(as.character(input$Slider),
               "2000" = "00",
               "2001" = "01",
               "2002" = "02",
               "2003" = "03",
               "2004" = "04",
               "2005" = "05",
               "2006" = "06",
               "2007" = "07",
               "2008" = "08",
               "2009" = "09",
               "2010" = "10",
               "2011" = "11",
               "2012" = "12",
               "2013" = "13",
               "2014" = "14",
               "2015" = "15")  
      })
    
      output$plot <- renderPlot({
        get(paste0("CrimeData", crime2(), year2()))
      })
    })
    
    
    
    library(shiny)
    ui <- shinyUI(fluidPage(
      sidebarLayout( position = "right",
                     sidebarPanel(
                       selectInput("select", label = h3("Select Crime"),
                                   choices = list("Assault" = "Assault", "Burglary" = "Burglary", "Burglary" = "Burglary", "Grand Larceny" = "Grand Larceny", "Murder" = "Murder", "Rape" = "Rape", "Robbery" = "Robbery"))
                     ),
                     mainPanel(
                       textOutput("text1"),
                       h1("NYC Crime Over Time", align = "center"),
                       sliderInput("Slider", "Year", 2000, 2015, 2000),
                       plotOutput("plot")
                     ))))
    
    shinyApp(ui, server)