'Reset inputs' button in shiny app

前端 未结 4 1699
既然无缘
既然无缘 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 01:08

    First of all, your use of the observer is correct, but there is another way that's slightly nicer. Instead of

    observe({
      input$reset_input
      updateNumericInput(session, "mynumber", value = 20)
      updateTextInput(session, "mytext", value = "test")
    })
    

    You can change it to

    observeEvent(input$reset_input, {
      updateNumericInput(session, "mynumber", value = 20)
      updateTextInput(session, "mytext", value = "test")
    })
    

    Also note that you don't need to explicitly "return" from a renderText function, the last statement will automatically be used.


    Regarding the main question: Matthew's solution is great, but there's also a way to achieve what you want without having to move all your UI into the server. I think it's better practice to keep your UI in the UI file just because separation of structure and logic is generally a good idea.

    Full disclaimer: my solution involves using a package that I wrote. My package shinyjs has a reset function that allows you to reset an input or an HTML section back to its original value. Here is how to tweak your original code to your desired behaviour in a way that will scale to any number of inputs without having to add any code. All I had to do is add a call to useShinyjs() in the UI, add an "id" attribute to the form, and call reset(id) on the form.

    library(shiny)
    
    runApp(list(
    
      ui = pageWithSidebar(
    
        headerPanel("'Reset inputs' button example"),
    
        sidebarPanel(
          shinyjs::useShinyjs(),
          id = "side-panel",
          numericInput("mynumber", "Enter a number", 20),
          textInput("mytext", "Enter a text", "test"),
          tags$hr(),
          actionButton("reset_input", "Reset inputs")
        ),
    
        mainPanel(
          h4("Summary"),
          verbatimTextOutput("summary")
        )
    
      ),
    
      server = function(input, output, session) {
    
        output$summary <- renderText({
          return(paste(input$mytext, input$mynumber))
        })
    
        observeEvent(input$reset_input, {
          shinyjs::reset("side-panel")
        })
      }
    
    ))
    

提交回复
热议问题