简体   繁体   English

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

[英]R Shiny app closes by itself after a while when running from R Studio, but it's still listening… is this normal?

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

I've noticed that when I run my R Shiny app, everything I've programmed so far works as intended.我注意到,当我运行我的 R Shiny 应用程序时,到目前为止我编程的所有内容都按预期工作。 However, if I keep the window open in the background and switch to using something else (ie Excel, Chrome, etc.), or sometimes even if I'm on the window itself, after a few minutes or so the window just closes by itself. However, if I keep the window open in the background and switch to using something else (ie Excel, Chrome, etc.), or sometimes even if I'm on the window itself, after a few minutes or so the window just closes by本身。 However R Studio shows that it is still listening, and it won't run further code until I press the STOP button to terminate the existing run of the app.但是 R Studio 显示它仍在侦听,并且在我按下 STOP 按钮终止应用程序的现有运行之前,它不会运行进一步的代码。

Is this behavior normal in R Studio when developing the app, or does it suggest that there's something wrong with my code that's causing it to disappear?在开发应用程序时,R Studio 中的这种行为是否正常,或者是否表明我的代码有问题导致它消失? There are no warning messages that appear in the console when it disappears.控制台消失时不会出现警告消息。 I've tried running another app using some basic example code I found, and the same thing happens.我尝试使用我找到的一些基本示例代码运行另一个应用程序,并且发生了同样的事情。

If this is a common thing, why does it do that and is there any way to stop this?如果这是一件常见的事情,为什么要这样做,有什么办法可以阻止这种情况?

Example code probably isn't relevant, but here is an example app from the RStudio website https://shiny.rstudio.com/articles/basics.html . Example code probably isn't relevant, but here is an example app from the RStudio website https://shiny.rstudio.com/articles/basics.html . This issue occurs with this app too.此应用程序也会出现此问题。

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)

I wrote a few shiny apps myself, and I haven't encountered this behavior before, not even in RStudio or in my standalaone chrome app which uses no rstudio at all.我自己写了一些 shiny 应用程序,我以前没有遇到过这种行为,即使在 RStudio 或我的独立 chrome 应用程序中,它根本不使用 rstudio。

What I can see at a first glance, that you dont use a session object in your server.乍一看,您没有在服务器中使用 session object。

function(input, output, session)

While the session object handles reconnection with a server in the background, maybe it also does so with your localhost.虽然 session object 在后台处理与服务器的重新连接,但也许它也与您的本地主机一起处理。 But I really dont know.但我真的不知道。 Try to integrate this and if the error is still there, we have to look at your system and the installed packages in your app.尝试集成它,如果错误仍然存在,我们必须查看您的系统和应用程序中安装的包。

Session object Session object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM