3
votes

I want to know how to use a drop down menu to pass input arguments that are data frames that can then produce plots dynamically.

I have been able to create a shiny app that renders the UI correctly, but it always produces the plot associated with the 2012 data (no matter what year is selected from the drop down menu). What appears to be happening is that the renderPlot function I use is not recognizing the input$year argument, so my control flow just renders the last ggplot (after the "else" statement). My question, then, is how do I create an input from the drop down menu in my ui.R file that will be recognized by the server.R file and allow the user to dynamically render the plot based on the year they choose. Does this have to do with using the reactive() function? Any help would be greatly appreciated.

Here is the server.R code:

library(ggplot2)
library(plyr)
library(shiny)

business1986 <- readRDS("data/business1986.rds")
business1991 <- readRDS("data/business1991.rds")
business1999 <- readRDS("data/business1999.rds")
business2005 <- readRDS("data/business2005.rds")
business2012 <- readRDS("data/business2012.rds")

shinyServer(function(input, output){

    output$Plot <- renderPlot({
    if(input$year == "1986") {     
        ggplot(business1986, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
            labs(title="Number of Workers in Orange County by Sector, 1986") +
            theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
            theme(legend.title = element_text(size=13)) +
            scale_y_continuous(labels = comma) + scale_fill_brewer() 
    } else if(input$year == "1991") {
        ggplot(business1991, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
            labs(title="Number of Workers in Orange County by Sector, 1991") +
            theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
            theme(legend.title = element_text(size=13)) +
            scale_y_continuous(labels = comma) + scale_fill_brewer() 
    } else if(input$year == "1999"){
        ggplot(business1999, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
            labs(title="Number of Workers in Orange County by Sector, 1999") +
            theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
            theme(legend.title = element_text(size=13)) +
            scale_y_continuous(labels = comma) + scale_fill_brewer() 
    } else if(input$year == "2005") {
        ggplot(business2005, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
            labs(title="Number of Workers in Orange County by Sector, 2005") +
            theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
            theme(legend.title = element_text(size=13)) +
            scale_y_continuous(labels = comma) + scale_fill_brewer() 
    } else {
        ggplot(business2012, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
            labs(title="Number of Workers in Orange County by Sector, 2012") +
            theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
            theme(legend.title = element_text(size=13)) +
            scale_y_continuous(labels = comma) + scale_fill_brewer() 
    }
            })

    })

And here is the ui.R code:

shinyUI(fluidPage(
    titlePanel("Economic Change in Orange County, CA"),
    sidebarLayout(
        sidebarPanel(h3("Choose a Year"), position="left",
        helpText("Observe econommic change in Orange County between 1986 and 2010
                 by adjusting the controls below."),
        selectInput("year", label = h5("Select Year"), 
                    choices = list("1986" = 1, "1991" = 2,
                                   "1999" = 3, "2005" = 4,
                                   "2012" = 5), selected = 1)),
        mainPanel(h3("Business Sector Composition"), 
                  plotOutput("Plot"), 
                  position="right")
    )
    )
)
1
I can't say for sure since your didn't provide data for testing your code, but it looks like you are checking the names rather than the values of input$year in your if..else block. Instead of doing if(input$year == "1986"), try using if(input$year == 1), etc... and see if that works. - nrussell
Great, I'll try that - benpiggot
Thanks, that worked! Though the solution would be much more complicated! - benpiggot
@user3619356 your code would have worked if you had passed c("1986", "1991", "1999", "2005", "2012) to the choices argument, instead of a named list (btw you can use a named vector as well, calling c instead of list but passing in the same arguments... I think Shiny treats them the same way). But a named vector is better anyway, imo. - shadowtalker

1 Answers

2
votes

I used this code and it works for me in the same scenario as you have described. Reactive() is what I have used.

ui <- fluidPage(
  sidebarLayout(

  sidebarPanel(
  selectInput(inputId = "diamonds", 
              label = "choose a category in Diamonds", 
              choices = c("Length", "Breath", "Height"," Please Select a Type "),


  ))

)

server <- function(input, output){

  datasetInput <- reactive({ 
    switch(input$dataset, 
           "Length" = diamonds$x, 
           "Breath" = diamonds$y, 
           "Height" = diamonds$z,
           " Please Select a Type " = NULL) 
  })