Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

r - Restart Shiny Session

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
  })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...