19

This seems like a very obvious question but I haven't found anything on the subject.

How can I refresh a shiny application (the equivalent of pressing F5, or clicking the "Reload App" button in RStudio)?

ui.R

 shinyUI(pageWithSidebar(
  headerPanel("Example"),
  sidebarPanel(
    actionButton("goButton", "Refresh")
  ),  
  mainPanel(
        h4("I would really like to refresh this please.")
    )   
))

server.R

shinyServer(function(input, output,session) { 
  observe({
    if(input$goButton==0) return(NULL)
    isolate({
      #
      #  I would like to refresh my session here with some sort of 
      #  function like session(refresh)...
    })
    })
})

I don't think I want to use stopApp() - I just want to refresh it such that it's in the same state as when it is loaded.

UPDATE

On the RStudio website, it shows here how to manage a user's session from the server. Specifically,

$ sudo rstudio-server suspend-session <pid>

Is there the equivalent function as the user, from within the app? In the documentation for session info (here), it says there is an onSessionEnded(callback) function. It would be good if there was a session.End() function which performs the above suspend-session function!

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
Hamilton Blake
  • 622
  • 1
  • 6
  • 19

3 Answers3

23

The session has now a method to do the trick. Shinyjs is no longer required:

session$reload()
Jan
  • 4,974
  • 3
  • 26
  • 43
  • 3
    Which requires adding `session` to the server's `function()` arguments. – stragu Mar 02 '22 at 00:59
  • If I have a shinydashboard with multiple tibItems and I would like to refresh a specific tabItem without leave this TabItem panel? – Laura Sep 29 '22 at 16:05
  • That is an entirely different question. There are several questions about such scenarios here at SO. If none of those answer your question, you can ask your own one. Without further details my first thought would be to put all tab content in a `uiOutput` element and get the content from the server with `renderUI`. – Jan Sep 29 '22 at 16:49
20

You can use the history.go(0) js-method to reload the page and thus reset the session, e.g. from a link:

p(HTML("<A HREF=\"javascript:history.go(0)\">Reset this page</A>"))

Furthermore you can use the shinyjs package to execute javascript from within the server:

library(shiny)
library(shinyjs)

jsResetCode <- "shinyjs.reset = function() {history.go(0)}" # Define the js method that resets the page

shinyApp(
  ui = fluidPage(
    useShinyjs(),                                           # Include shinyjs in the UI
    extendShinyjs(text = jsResetCode, functions = "reset"), # Add the js code to the page
    actionButton("reset_button", "Reset Page")
  ),

  server = function(input, output) {
    observeEvent(input$reset_button, {js$reset()})          # Call the method from
                                                            # somewhere within the server
  })
DeanAttali
  • 25,268
  • 10
  • 92
  • 118
bubble
  • 412
  • 6
  • 11
  • 6
    Nice answer @bubble. As the author of shinyjs, I'd also like to add that if resetting all the inputs is sufficient (ie. get all the input values back to their initial state, instead of literally refreshing the page), then you can use the built-in shinyjs reset function – DeanAttali Dec 05 '15 at 03:12
  • Thank you for the feedback @daattali :) It was my first answer. – bubble Dec 07 '15 at 10:31
  • 2
    Nice answer, sorry for the delay. – Hamilton Blake May 19 '16 at 12:50
9

You can add a refresh icon and popover to your app by including the following in your ui code:

library(shinyBS)

tags$a(href="javascript:history.go(0)", 
           popify(tags$i(class="fa fa-refresh fa-5x"),
                  title = "Reload", 
                  content = "Click here to restart the Shiny session",
                  placement = "right"))

It should give you this:

enter image description here

SANBI samples
  • 2,058
  • 2
  • 14
  • 20