5

I tried to implement a page refresh button following the link here. However when I tried deploying to shinyapp.io, it failed and asked for installing package V8 which I had already done. The app was working fine on the machine. The code I used is:

jsResetCode <- "shinyjs.reset = function() {history.go(0)}",

useShinyjs(), # Include shinyjs in the UI

extendShinyjs(text = jsResetCode), # Add the js code to the page   

p(actionButton("reset_button", "Reset Tool"))

In server.R:

observeEvent(input$reset_button, {js$reset()}) 

Is there any way to do this without shinyjs?

Jim
  • 256
  • 5
  • 16
Rajarshi Bhadra
  • 1,826
  • 6
  • 25
  • 41
  • Read the "Note about V8 prerequisite" in the shinyjs [readme](https://github.com/daattali/shinyjs#note-about-v8-prerequisite). "If you are deploying an app that uses extendShinyjs to shinyapps.io then you need to add a call to library(V8) somewhere in your code. This is necessary because the shinyapps.io server needs to know that it should install the V8 package. If you do not do this then you will simply see an error saying the package is missing." – DeanAttali Dec 07 '15 at 22:20
  • I called V8 at the outset only. even then it is not working after deployment in shinyapps.io – Rajarshi Bhadra Dec 08 '15 at 04:38
  • What does it mean "I called it at the outset only"? Is there a `library(V8)` call somewhere in code? If so, you shouldn't have any problems – DeanAttali Dec 08 '15 at 06:02
  • Yeah it worked. Somehow it wasn't working initially. Maybe a refresh issue – Rajarshi Bhadra Dec 08 '15 at 06:07
  • Perhaps you would like to accept Dean Attali's answer. The current version contains a non-`shinyjs` way of refreshing the page. – Jim Aug 03 '18 at 18:35

1 Answers1

25

Edit: Since shiny version 0.13.0, it's possible to refresh the page using Shiny's session$reload() function


Original answer before 0.13.0 (you can still use this, but it's not needed)

The code below is a minimal example of a working Shiny app that uses a "refresh" button

library(shiny)
library(shinyjs)

jscode <- "shinyjs.refresh_page = function() { history.go(0); }"

ui <- fluidPage(
  useShinyjs(),
  extendShinyjs(text = jscode, functions = "refresh_page"),
  textInput("text", "Text"),
  actionButton("refresh", "Refresh app")
)

server <- function(input, output, session) {
  observeEvent(input$refresh, {
    js$refresh_page();
  })
}

shinyApp(ui = ui, server = server)
DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • Hi, thanks for your input, I had the same question and it worked very well by following your answer. I am wondering how would I implement your code to create page refresh buttons on multiple tabs. I posted my question here: https://stackoverflow.com/questions/55262781/r-shiny-page-refresh-button-for-multiple-tabs. I greatly appreciate your help! – MMAASS Mar 20 '19 at 14:18
  • 1
    The edit is helpful: just use session$reload() – dca Apr 08 '21 at 15:32
  • Yeah love the edit. I would put it at the top so its the first thing people see. I started installing shinyjs haha – Avery Robbins Apr 29 '21 at 02:57
  • This post is old, as of now, `functions` argument must be provided. When I supply the name of the function, I receive: `functions` argument must not contain any of the following function names:` and lists the function name I had defined in the jscode string. – kraggle Feb 22 '23 at 20:29
  • 1
    @kraggle You're right, I updated that. I also moved the note that session$reload() is better to the top – DeanAttali Feb 23 '23 at 20:24