重启Shiny Session

Ham*_*ake 13 r shiny shinyjs

这似乎是一个非常明显的问题,但我还没有找到关于这个主题的任何内容.

如何刷新闪亮的应用程序(相当于按下F5,或单击RStudio中的"重新加载应用程序"按钮)?

ui.R

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

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)...
    })
    })
})
Run Code Online (Sandbox Code Playgroud)

我不认为我想使用stopApp() - 我只想刷新它,使其处于加载时的状态.

UPDATE

在RStudio网站上,它显示如何从服务器管理用户的会话.特别,

$ sudo rstudio-server suspend-session <pid>
Run Code Online (Sandbox Code Playgroud)

应用程序中是否存在与用户相同的功能?在会话信息的文档中(这里),它说有一个onSessionEnded(回调)函数.如果有session.End()函数执行上面的suspend-session函数会很好!

Jan*_*Jan 14

会话现在有一个方法来解决这个问题。不再需要 Shinyjs:

session$reload()
Run Code Online (Sandbox Code Playgroud)

  • 这需要将“session”添加到服务器的“function()”参数中。 (3认同)

bub*_*ble 11

您可以使用history.go(0)js-method重新加载页面,从而重置会话,例如从链接:

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

此外,您可以使用该shinyjs从服务器中执行javascript:

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),                      # 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
  })
Run Code Online (Sandbox Code Playgroud)

  • 很好的答案@bubble.作为shinyjs的作者,我还想补充一点,如果重置所有输入就足够了(即将所有输入值恢复到初始状态,而不是逐字刷新页面),那么你可以使用内置的 - 在shinyjs重置功能 (3认同)
  • 很好的答案,抱歉耽误了。 (2认同)

SAN*_*les 6

您可以通过在您的 ui 代码中包含以下内容,为您的应用添加刷新图标和弹出框:

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"))
Run Code Online (Sandbox Code Playgroud)

它应该给你这个:

在此处输入图片说明