Run
Run

Reputation: 57256

Shiny - how to reset/ refresh the form?

How can I refresh or reset the ui/ form in Shiny?

I have this button in ui.R:

actionButton("resetInput", "Reset inputs")

What should I do in the server.R to reset the form?

observeEvent(input$resetInput, {
   // refresh or reset the form      
})

I tried this answer, but I get this error:

Warning: Error in library: there is no package called ‘shinyjs’

Does this package really exist?

Any better way of doing it without installing new packages?

Upvotes: 1

Views: 1651

Answers (1)

Florian
Florian

Reputation: 25405

You should put

library(shinyjs)

above your server definition, which is missing in the example you are referring to.

So:

library(shinyjs)
library(shiny)
runApp(shinyApp(
  ui = fluidPage(
    shinyjs::useShinyjs(),
    div(
      id = "form",
      textInput("text", "Text", ""),
      selectInput("select", "Select", 1:5),
      actionButton("refresh", "Refresh")
    )
  ),
  server = function(input, output, session) {
    observeEvent(input$refresh, {
      shinyjs::reset("form")
    })
  }
))

I will modify the answer you are referring to to also include the library call. Hope this helps!

Upvotes: 2

Related Questions