'Reset inputs' button in shiny app

前端 未结 4 1675
既然无缘
既然无缘 2020-11-29 00:38

I would like to implement a \'Reset inputs\' button in my shiny app.

Here is an example with just two inputs where I\'m using the update functions to set the values

4条回答
  •  北海茫月
    2020-11-29 00:58

    Here is yet another option that works for either static or dynamic inputs, and doesn't involve re-rendering inputs entirely.

    It uses:

    reactiveValuesToList to get all initial input values, and (optionally) any dynamic input values that get initialized afterward.

    session$sendInputMessage to update values for generic inputs. The updateXyzInput functions call this under the hood like session$sendInputMessage(inputId, list(value = x, ...).

    Every Shiny input uses value for its input message, and almost all will update with their input value as-is. Only a two inputs I've found need special casing - checkboxGroupInput to not send NULL when nothing is checked, and dateRangeInput to convert its c(start, end) to a list(start = start, end = end).

    It may not be a good idea to blindly reset ALL inputs (even tabs will be reset), but this can easily be adapted to reset a filtered set of inputs.

    library(shiny)
    
    ui <- pageWithSidebar(
      headerPanel("'Reset inputs' button example"),
    
      sidebarPanel(
        numericInput("mynumber", "Enter a number", 20),
        textInput("mytext", "Enter text", "test"),
        textAreaInput("mytextarea", "Enter text", "test"),
        passwordInput("mypassword", "Enter a password", "password"),
        checkboxInput("mycheckbox", "Check"),
        checkboxGroupInput("mycheckboxgroup", "Choose a number", choices = c(1, 2, 3)),
        radioButtons("myradio", "Select a number", c(1, 2, 3)),
        sliderInput("myslider", "Select a number", 1, 5, c(1,2)),
        uiOutput("myselUI"),
        uiOutput("mydateUI"),
        tags$hr(),
        actionButton("reset_input", "Reset inputs")
      ),
    
      mainPanel(
        h4("Summary"),
        verbatimTextOutput("summary")
      )
    )
    
    server <- function(input, output, session) {
    
      initialInputs <- isolate(reactiveValuesToList(input))
    
      observe({
        # OPTIONAL - save initial values of dynamic inputs
        inputValues <- reactiveValuesToList(input)
        initialInputs <<- utils::modifyList(inputValues, initialInputs)
      })
    
      observeEvent(input$reset_input, {
        for (id in names(initialInputs)) {
          value <- initialInputs[[id]]
          # For empty checkboxGroupInputs
          if (is.null(value)) value <- ""
          session$sendInputMessage(id, list(value = value))
        }
      })
    
      output$myselUI <- renderUI({
        selectInput("mysel", "Select a number", c(1, 2, 3))
      })
    
      output$mydateUI <- renderUI({
        dateInput("mydate", "Enter a date")
      })
    
      output$summary <- renderText({
        return(paste(input$mytext, input$mynumber))
      })
    }
    
    shinyApp(ui, server)
    

提交回复
热议问题