Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Created February 18, 2016 18:59
Show Gist options
  • Save aagarw30/4bc6e6fee0cb707e2755 to your computer and use it in GitHub Desktop.
Save aagarw30/4bc6e6fee0cb707e2755 to your computer and use it in GitHub Desktop.
Demo updateSelectInput in R Shiny
library(shiny)
shinyServer(function(input, output, session){
observe(
{input$Year
# Update based on the year change event
updateSelectInput(session, "Month", "Month", choices = data$Month[data$Year==input$Year])
}
)
observe(
{
input$Month
# Update based on the month change event
updateSelectInput(session, "Person", "Person",
choices = data$Name[data$Month==input$Month & data$Year==input$Year])})
})
library(shiny)
# Creating a dummy dataset just for demo sake
data = data.frame(Year=c("2002","2003","2004","2003","2001","2002", "2001"),
Month = c("Jan", "Feb", "Mar", "Jan", "Dec", "Jan", "Nov"),
Name =c("Sam", "Paul", "James", "Ana","Rose", "Juan", "Tim"),
row.names=NULL, stringsAsFactors = FALSE)
data$Year = factor(data$Year, levels = c("2001", "2002", "2003", "2004"))
shinyUI(fluidPage(
selectInput("Year", "Year", choices = levels(data$Year) ,
selected = levels(data$Year)[1]),
# Choice of this select input based on the first year that appears on level & which was actaully selected in the year select input
selectInput("Month", "Month", choices = data$Month[data$Year==levels(data$Year)[1]]),
# Choice of this select input based on the first year that appears on level & which was actaully selected in the year select input
selectInput("Person", "Person", choices = data$Name[data$Year==levels(data$Year)[1]])
)
)
@gcmicah
Copy link

gcmicah commented Mar 1, 2018

I got the following error msg when trying to run your code. Any suggestion on how to fix?

Warning: Error in $: object of type 'closure' is not subsettable
Stack trace (innermost first):
    57: updateSelectInput
    56: observerFunc [C:\Users\s\R\testInput/server.R#7]
     1: shiny::runApp
Warning: Error in $: object of type 'closure' is not subsettable
Stack trace (innermost first):
    57: updateSelectInput
    56: observerFunc [C:\Users\s\R\testInput/server.R#15]
1: shiny::runApp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment