Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a page refresh button by using R Shiny

Tags:

r

shiny

I'm making an app and I need to add a button to refresh page (same function to press F5). Is there anyone can share a piece of code to implement it?

Thanks a lot!

like image 640
Mark Zhou Avatar asked Jun 15 '15 18:06

Mark Zhou


1 Answers

I do have a very simple and nice solution but it won't work for a file input.

Here's a solution that'll work for all inputs except a file input:

UPDATE 2017: this solution did not work on file inputs for the first 2 years, but it does now.

library(shiny)
library(shinyjs)
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")
    })
  }
))

When you press "Refresh", all inputs will be reset to their initial values.

But file inputs are very strange and it's hard to "reset" them. See here. You could hack some JavaScript together to try to almost kind of reset an input field if you want. Here's how you would perform an actual page refresh:

library(shiny)
library(shinyjs)
runApp(shinyApp(
  ui = fluidPage(
    shinyjs::useShinyjs(),
    shinyjs::extendShinyjs(text = "shinyjs.refresh = function() { location.reload(); }"),
    textInput("text", "Text", ""),
    actionButton("refresh", "Refresh")
  ),
  server = function(input, output, session) {
    observeEvent(input$refresh, {
      shinyjs::js$refresh()
    })
  }
))

Disclaimer: both these solutions use a package I wrote, shinyjs

like image 74
DeanAttali Avatar answered Oct 17 '22 10:10

DeanAttali