简体   繁体   English

从两个R会话同时访问环境

[英]Simultaneously access environment from two R sessions

Is it technically possible in R? R在技术上是否可行?

I would like to run a shiny instance with prepared R6 object (environment class), use its methods - mostly read only. 我想用准备好的R6对象(环境类)运行一个闪亮的实例,使用它的方法 - 大多数只读。
While at the same time as shiny app running I would like to call other methods of my R6 - read/write. 虽然在运行闪亮的应用程序的同时我想调用我的R6的其他方法 - 读/写。
Shiny R session could be a host for my R6 object while the second session would be called from scheduled R script / interactively from R console. Shiny R会话可以是我的R6对象的主机,而第二个会话将从R控制台以预定的R脚本/交互方式调用。
Currently what I think I can do is to source R script directly from shiny under a button, but this limits interactivity. 目前我认为我可以做的是直接从闪烁的按钮中获取R脚本,但这限制了交互性。

This article describes the scoping rules for Shiny apps and how to define global data with variously packaged code. 本文介绍了Shiny应用程序范围规则以及如何使用各种打包的代码定义全局数据。 Below is my example of a global variable holding data accessible to multiple sessions. 下面是我保存多个会话可访问数据的全局变量的示例。

Run this app, then open a second tab/window in your browser and point it at the same connection. 运行此应用程序,然后在浏览器中打开第二个选项卡/窗口并将其指向同一连接。 You can click the +1 button in one session to increment the shared max and local count. 您可以在一个会话中单击+1按钮以增加共享的最大和本地计数。 In the other session, you will not see any change until something triggers shiny to re-check the shared data, but clicking the +1 button there will update the local count, but also trigger update of the shared max data value. 在另一个会话中,您不会看到任何更改,直到某些内容触发闪亮以重新检查共享数据,但单击+1按钮将更新本地计数,但也会触发更新共享最大数据值。 You can click the +1 button there several times until you have a new max, then go back to the first window and you can see the max is visible there too, once you click the +1 button to trigger an update. 您可以多次单击+1按钮,直到获得新的最大值,然后返回第一个窗口,一旦您单击+1按钮触发更新,您就可以看到最大值也可见。 This works for multiple windows. 这适用于多个窗口。

You have to do something to make a session check the data again to update. 您必须做一些事情才能使会话再次检查数据以进行更新。 I didn't work too hard to make this happen without side effects. 如果没有副作用,我没有太努力地做到这一点。 There should be some way to do a "refresh" based on whatever trigger you want. 应该有一些方法可以根据你想要的任何触发器进行“刷新”。 You can even use a timed poll to keep data in sync, like this example does with files. 您甚至可以使用定时轮询来保持数据同步,就像此示例对文件一样。

The one caveat to this example is I have only tried with a local RStudio shiny server implementation. 这个例子的一个警告是我只尝试使用本地RStudio闪亮服务器实现。 I don't know that it works this way on a real server. 我不知道它在真实的服务器上以这种方式工作。 If it does not, please comment to that effect! 如果没有,请评论这个效果! It will probably not work this way on shinyapps.io or with any kind of cloud/load-balancing in general as you can't guarantee that two sessions share an app instance on one machine. 它可能不会在shinyapps.io或通常使用任何类型的云/负载平衡上以这种方式工作,因为您无法保证两个会话在一台计算机上共享应用程序实例。

library(shiny)

globalMax <- 0

app <- shinyApp(
   ui= pageWithSidebar(
      headerPanel("Shared data demo"),
      sidebarPanel(
         actionButton("plusButton", "+1")
      ),
      mainPanel(
         verbatimTextOutput("sharedMax")
      )
   ),
   server= function(input,output){
      observe({
         if (input$plusButton > globalMax) {
            globalMax <<- input$plusButton
         }
      })
      output$sharedMax <- renderText({
         paste0( "Shared max value: ",  globalMax, "\n",
                 "Local value: ",  input$plusButton)
      })
   }
)

runApp(app)

I think I have done what you want accidentally - essentially 2 RStudio sessions running in parallel? 我想我已经意外地做了你想做的事 - 基本上是2个并行运行的RStudio会话? On my Mac I just right-click RStudio and select New RStudio Window. 在我的Mac上,我只需右键单击RStudio并选择New RStudio Window。 I have done it on Windows as well but don't have access to one atm - should be similar. 我也在Windows上完成了它,但是没有访问一个atm - 应该是类似的。 Running 2 sessions allows you to preserve and refer back to the original while you tweak the 2nd instance. 运行2个会话允许您在调整第2个实例时保留并返回原始文件。 With 2 monitors, this can be very handy for speeding up dev time on finicky details ;) 有2个显示器,这对于加快精简细节的开发时间非常方便;)

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

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