重启Shiny Session

时间:2014-07-31 15:04:16

标签: 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.")
    )   
))

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

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

更新

在RStudio网站上,它显示here如何从服务器管理用户的会话。具体来说,

$ sudo rstudio-server suspend-session <pid>

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

3 个答案:

答案 0 :(得分:9)

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

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

此外,您可以使用shinyjs package从服务器中执行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
  })

答案 1 :(得分:6)

会话现在有一种方法可以解决问题。不再需要Shinyjs:

session$reload()

答案 2 :(得分:4)

您可以通过在您的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"))

应该给你这个:

enter image description here

相关问题