1

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?

Florian
  • 24,425
  • 4
  • 49
  • 80
Run
  • 54,938
  • 169
  • 450
  • 748
  • 1
    The package does exist [here is a link](https://cran.r-project.org/web/packages/shinyjs/index.html). I have just installed it on my machine using `install.packages("shinyjs")`. Are you using the latest version of R? – Michael Bird Jul 25 '17 at 12:06
  • @MichaelBird yes I am. `R version 3.4.0` does it need the latest version R? – Run Jul 25 '17 at 12:17
  • 1
    According to CRAN it needs R >3.1.0 so that's not the issue. The error suggests it is a library issue, other than that, I don't know. – Michael Bird Jul 25 '17 at 12:21
  • alternatively you can use `updateSelectInput` to reset values after `actionbutton` trigger – parth Jul 25 '17 at 12:39
  • @parth any example please? – Run Jul 25 '17 at 12:49

1 Answers1

2

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!

Florian
  • 24,425
  • 4
  • 49
  • 80