Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny: Make renderUI react to drop-down list not submitButton

Tags:

r

shiny

How do I get renderUI to react to the user selecting different values from a drop-down list without having to click on my submitButton?

I've got a wellPanel that contains 3 things:
1) My drop-down list
2) A set of dynamic inputs (created by my renderUI function, depend on selection in #1)
3) submitButton

Desired behavior: Changes to the drop-down selection give the user different input widgets to use. When they are ready for the results of their selected inputs, they click the submitButton, and they get their results in the mainPanel.

Problem: My renderUI only reacts to the drop-down selection after clicking the submitButton. As far as I can tell, I need to isolate something or use observeEvent, but I haven't been able to figure it out.

Simplified example:

rm(list = ls())
library(shiny)

ui  <- fluidPage(
  fluidRow(
column(4,wellPanel(
  selectInput("analysis", label = "Type of Analysis:", 
              c("Award Total" = "total", 
                "Award Average" = "average"),
              width = validateCssUnit("70%")),
  uiOutput("filter_box"),
  submitButton()
    )),
column(8, textOutput("sample_text"))
  )
)

server <- function(input, output, session){

  output$filter_box <- renderUI({
if(input$analysis == "total"){
  tagList(radioButtons(inputId = "input1", label = "Select One:",c("A", "B", "C"), selected = "A"))
} else {
  tagList(checkboxGroupInput(inputId = "input2", label = "Select all that apply:",c("1","2","3","4","5")),
          dateRangeInput(inputId = "input3", label = "Enter Date Range"))
}
})

output$sample_text <- renderText({
  if(input$analysis == "total"){
    input$input1
  } else if(input$analysis == "average") {
    c(input$input2, input$input3)
  }
 })
}

runApp(list(ui = ui, server = server))
like image 836
twgardner2 Avatar asked Dec 31 '16 01:12

twgardner2


1 Answers

You need to introduce two changes.

  1. Change the submitButton to an actionButton (see comment from @daattali)

  2. Isolate renderText, and make it reactive to the actionButton.

See code below.

rm(list = ls())
library(shiny)

ui  <- fluidPage(
  fluidRow(
    column(4,wellPanel(
      selectInput("analysis", label = "Type of Analysis:", 
                  c("Award Total" = "total", 
                    "Award Average" = "average"),
                  width = validateCssUnit("70%")),
      uiOutput("filter_box"),
      actionButton(inputId = 'button_1',label = 'Apply Changes')
    )),
    column(8, textOutput("sample_text"))
  )
)

server <- function(input, output, session){

  output$filter_box <- renderUI({
    if(input$analysis == "total"){
      tagList(radioButtons(inputId = "input1", label = "Select One:",c("A", "B", "C"), selected = "A"))
    } else {
      tagList(checkboxGroupInput(inputId = "input2", label = "Select all that apply:",c("1","2","3","4","5")),
              dateRangeInput(inputId = "input3", label = "Enter Date Range"))
    }
  })

  output$sample_text <- renderText({
    input$button_1
    isolate({
      if(input$analysis == "total"){
        input$input1
      } else if(input$analysis == "average") {
        c(input$input2, input$input3)
      }
    })

  })
}

runApp(list(ui = ui, server = server))
like image 122
Eduardo Bergel Avatar answered Nov 11 '22 01:11

Eduardo Bergel