连接多个用户的 R 闪亮会话 [英] R shiny session with multiple users connected

查看:43
本文介绍了连接多个用户的 R 闪亮会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最小可重现示例:

library("shiny")

ui <- fluidPage(
  actionButton("button1", "Run 1"),
  actionButton("button2", "Run 2")
)

server <- function(session, input, output) {
  cat("session starts\n")
  observeEvent(input$button1, {
    cat("1 starts\n")
    Sys.sleep(15)
    cat("1 stops\n")
  })

  observeEvent(input$button2, {
    cat("2 starts\n")
    Sys.sleep(15)
    cat("2 stops\n")
  })
}

shinyApp(ui = ui, server = server)

每个按钮模拟运行一些长时间的 CPU 密集型算法.

Each button simulates running some long cpu-intensive algorithm.

  1. 运行应用并在一个浏览器标签上打开一个会话.
  2. 为正在运行的应用打开另一个包含另一个会话的浏览器标签页.
  3. 在第一个标签中开始运行 1.转到第二个浏览器选项卡并启动运行 2.
  1. Run the app and open a session on one browser tab.
  2. Open another browser tab with another session for the running app.
  3. Start Run 1 in the first tab. Go to the second browser tab and start the Run 2.

问题:第二个按钮观察器不会独立启动.它一直等到第一次运行在第一个会话中完成.我认为闪亮的会话是独立的.闪亮的每个 R 会话如何处理多个闪亮的会话?如果多个用户想要同时连接到应用程序怎么办?

The problem: The second button observer does not start independently. It waits until the first run is finished in the first session. I thought that shiny sessions are independent. How does shiny handle multiple shiny sessions per single R session? What if multiple users want to connect to the application at the same time?

如何处理多个用户同时运行同一个应用程序?谢谢

How does one handle multiple users running the same app at the same time? Thanks

推荐答案

限制每个工作进程的连接数,即给每个用户自己的 R 工作进程.您可以通过将每个工作进程允许的并发连接数设置为 1 来实现这一点.

Limit the number of connections per worker process, i.e. give each user their own R worker process. You can do this by setting the number of concurrent connections allowed per worker process to 1.

如果您通过 Shinyapps.io 部署您的应用程序,说明和进一步的背景在这里:https://shiny.rstudio.com/articles/scaling-and-tuning.html

If you are deploying your app via shinyapps.io the instructions and further background is here: https://shiny.rstudio.com/articles/scaling-and-tuning.html

如果您要部署到自己的闪亮服务器,请参阅此处的说明和更多背景信息:https://support.rstudio.com/hc/en-us/articles/220546267-Scaling-and-Performance-Tuning-Applications-in-Shiny-Server-Pro

If you are deploying to your own shiny server the instructions and further background is here: https://support.rstudio.com/hc/en-us/articles/220546267-Scaling-and-Performance-Tuning-Applications-in-Shiny-Server-Pro

我使用了您的应用,添加了一个可见的进度条,并使用上述设置进行部署:https://minhealthnz.shinyapps.io/example_parallel/

I took your app, added a visible progress bar, and deployed it with the above settings: https://minhealthnz.shinyapps.io/example_parallel/

library("shiny")

ui <- fluidPage(
  actionButton("button1", "Run 1"),
  actionButton("button2", "Run 2")
)

server <- function(session, input, output) {

  observeEvent(input$button1, {

    withProgress(message = 'Run 1', detail = '', value = 0, {
        for (i in 1:15) {
            incProgress(1/15)
            Sys.sleep(0.25)
        }
    })

  })

  observeEvent(input$button2, {

    withProgress(message = 'Run 2', detail = '', value = 0, {
        for (i in 1:15) {
            incProgress(1/15)
            Sys.sleep(0.25)
        }
    })

  })
}

shinyApp(ui = ui, server = server)

这篇关于连接多个用户的 R 闪亮会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆