'Reset inputs' button in shiny app

前端 未结 4 1702
既然无缘
既然无缘 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:59

    There isn't such a function in shiny, however, here's a way to accomplish this without having to essentially define your inputs twice. The trick is to use uiOutput and wrap the inputs you want to reset in a div whose id changes to something new each time the reset button is pressed.

    library(shiny)
    
    runApp(list(
    
      ui = pageWithSidebar(
    
        headerPanel("'Reset inputs' button example"),
    
        sidebarPanel(
          uiOutput('resetable_input'),
          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))
        })
    
        output$resetable_input <- renderUI({
            times <- input$reset_input
            div(id=letters[(times %% length(letters)) + 1],
                numericInput("mynumber", "Enter a number", 20),
                textInput("mytext", "Enter a text", "test"))
        })
    
      }
    ))
    

提交回复
热议问题