Created
February 18, 2016 18:59
-
-
Save aagarw30/4bc6e6fee0cb707e2755 to your computer and use it in GitHub Desktop.
Demo updateSelectInput in R Shiny
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]) | |
) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got the following error msg when trying to run your code. Any suggestion on how to fix?