微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 R Studio 运行时,R Shiny 应用程序会在一段时间后自行关闭,但它仍在收听……这正常吗?

如何解决从 R Studio 运行时,R Shiny 应用程序会在一段时间后自行关闭,但它仍在收听……这正常吗?

关于 R Shiny/R Studio 的一般问题...

我注意到,当我运行 R Shiny 应用程序时,到目前为止我编写的所有程序都按预期工作。但是,如果我在后台保持窗口打开并切换到使用其他东西(即 Excel、Chrome 等),或者有时即使我在窗口本身,几分钟后窗口就会关闭本身。然而,R Studio 显示它仍在侦听,并且在我按下 STOP 按钮终止应用程序的现有运行之前它不会运行更多代码

在开发应用程序时,这种行为在 R Studio 中是否正常,或者是否表明我的代码有问题导致它消失?控制台消失时不会出现警告消息。我尝试使用我找到的一些基本示例代码运行另一个应用程序,并且发生了同样的事情。

如果这是常见的事情,为什么要这样做,有什么办法可以阻止这种情况?

示例代码可能不相关,但这里是来自 RStudio 网站 https://shiny.rstudio.com/articles/basics.html 的示例应用。此应用也会出现此问题。

ui <- fluidPage(
  
  # App title ----
  titlePanel("Reactivity"),# Sidebar layout with input and output deFinitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Text for providing a caption ----
      # Note: Changes made to the caption in the textInput control
      # are updated in the output area immediately as you type
      textInput(inputId = "caption",label = "Caption:",value = "Data Summary"),# Input: Selector for choosing dataset ----
      selectInput(inputId = "dataset",label = "Choose a dataset:",choices = c("rock","pressure","cars")),# Input: Numeric entry for number of obs to view ----
      numericInput(inputId = "obs",label = "Number of observations to view:",value = 10)
      
    ),# Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Formatted text for caption ----
      h3(textoutput("caption",container = span)),# Output: Verbatim text for data summary ----
      verbatimtextoutput("summary"),# Output: HTML table with requested number of observations ----
      tableOutput("view")
      
    )
  )
)

# Define server logic to summarize and view selected dataset ----
server <- function(input,output) {
  
  # Return the requested dataset ----
  # By declaring datasetInput as a reactive expression we ensure
  # that:
  #
  # 1. It is only called when the inputs it depends on changes
  # 2. The computation and result are shared by all the callers,#    i.e. it only executes a single time
  datasetInput <- reactive({
    switch(input$dataset,"rock" = rock,"pressure" = pressure,"cars" = cars)
  })
  
  # Create caption ----
  # The output$caption is computed based on a reactive expression
  # that returns input$caption. When the user changes the
  # "caption" field:
  #
  # 1. This function is automatically called to recompute the output
  # 2. New caption is pushed back to the browser for re-display
  #
  # Note that because the data-oriented reactive expressions
  # below don't depend on input$caption,those expressions are
  # NOT called when input$caption changes
  output$caption <- renderText({
    input$caption
  })
  
  # Generate a summary of the dataset ----
  # The output$summary depends on the datasetInput reactive
  # expression,so will be re-executed whenever datasetInput is
  # invalidated,i.e. whenever the input$dataset changes
  output$summary <- renderPrint({
    dataset <- datasetinput()
    summary(dataset)
  })
  
  # Show the first "n" observations ----
  # The output$view depends on both the databaseInput reactive
  # expression and input$obs,so it will be re-executed whenever
  # input$dataset or input$obs is changed
  output$view <- renderTable({
    head(datasetinput(),n = input$obs)
  })
  
  
}
shinyApp(ui,server)

解决方法

我自己编写了一些闪亮的应用程序,我以前从未遇到过这种行为,甚至在 RStudio 或我的完全不使用 rstudio 的独立 chrome 应用程序中也没有遇到过。

乍一看,您没有在服务器中使用会话对象。

function(input,output,session)

虽然会话对象在后台处理与服务器的重新连接,但它可能也会与您的本地主机进行重新连接。但我真的不知道。尝试集成它,如果错误仍然存​​在,我们必须查看您的系统和应用中已安装的软件包。

Session object

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。