16

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

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

用户界面

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

服务器.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 网站上,它在这里展示了如何从服务器管理用户的会话。具体来说,

$ sudo rstudio-server suspend-session <pid>

在应用程序中是否有与用户等效的功能?在会话信息(此处)的文档中,它说有一个 onSessionEnded(callback) 函数。如果有一个 session.End() 函数来执行上述挂起会话功能,那就太好了!

4

3 回答 3

19

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

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

此外,您可以使用该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, 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
  })
于 2015-12-02T08:44:13.040 回答
16

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

session$reload()
于 2020-05-23T19:42:25.780 回答
8

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

它应该给你这个:

在此处输入图像描述

于 2017-05-23T14:19:39.613 回答