Skip to content

Instantly share code, notes, and snippets.

@tcash21
Last active December 21, 2015 01:09
Show Gist options
  • Save tcash21/6225683 to your computer and use it in GitHub Desktop.
Save tcash21/6225683 to your computer and use it in GitHub Desktop.
DynamicUI example in Shiny
require(shiny)
require(rCharts)
inputChoices <- c("A", "B", "C", "D")
shinyServer(function(input, output, session){
input2Choices <- reactive({
inputChoices[-grep(input$input1, inputChoices)]
})
output$input1 <- renderUI({
selectInput("input1", "Input 1:", choices=as.list(inputChoices))
})
output$input2 <- renderUI({
if(is.null(input$input1))
return()
selectInput("input2", "Input 2:", choices=input2Choices())
})
carrierInput1 <- reactive({
switch(input$input1,
"A" = 1,
"B" = 2,
"C" = 3,
"D" = 4)
})
carrierInput2 <- reactive({
if(is.null(input$input2))
return()
switch(input$input2,
"A" = 1,
"B" = 2,
"C" = 3,
"D" = 4)
})
output$show <- renderPrint({
if(is.null(input$input1))
return()
print(carrierInput1())
})
output$show2 <- renderPrint({
if(is.null(input$input2)){
return()
}
if(input$input1 == input$input2){
return()
}
print(carrierInput2())
Sys.sleep(2)
})
})
shinyUI(pageWithSidebar(
headerPanel("Dynamic UI inputs with Shiny"),
sidebarPanel(
uiOutput('input1'),
uiOutput('input2'),
submitButton('Submit')
),
mainPanel(
verbatimTextOutput("show"),
verbatimTextOutput("show2")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment