简体   繁体   English

运行闪亮的应用程序时访问/使用 R 控制台

[英]Access/use R console when running a shiny app

Does anybody know if one is able to access the R console when running a shiny app?有人知道在运行闪亮的应用程序时是否可以访问 R 控制台吗? (running the shiny application in background would also be helpful, if that's possible) (如果可能的话,在后台运行闪亮的应用程序也会有所帮助)

I need this for manipulating objects in .GlobalEnv which are used in the shiny application and this has to be done using the command line.我需要这个来操作 .GlobalEnv 中的对象,这些对象在闪亮的应用程序中使用,这必须使用命令行来完成。

When starting the app the console is buzy.启动应用程序时,控制台嗡嗡作响。 Is there a possibility to access the console from within the application?是否可以从应用程序内部访问控制台?

Thanks in advance!提前致谢!

R (and shiny) run single-threaded. R(和闪亮的)运行单线程。 This thread is used by the shiny application so you cannot interact with R whenever the app is running.该线程由闪亮的应用程序使用,因此您无法在应用程序运行时与 R 交互。 If you want to run interactive commands during a shiny session you need to put a browser() inside your application like mentioned by @eric-canton.如果您想在闪亮的会话期间运行交互式命令,您需要像@eric-canton 提到的那样在您的应用程序中放置一个browser()

A very simple application could look like this一个非常简单的应用程序可能如下所示

library(shiny)

d <- data.frame(1:10, 1:10)

ui <-  fluidPage(
  actionButton("browser", "Trigger browser()"),
  actionButton("reload", "Reload Plot"),
  plotOutput("plot")
)


server <- function(input, output, session) {
  observeEvent(input$browser, {
    browser()
    1 + 1
  })

  output$plot <- renderPlot({
    input$reload
    plot(d)
  })
}

shinyApp(server = server, ui = ui)

Some comments about the code关于代码的一些评论

  • I put 1 + 1 after the browser() command because setting browser() as the last argument tends to stop the interactive terminal unexpectedly in my experience我将1 + 1放在browser()命令之后,因为根据我的经验,将browser()设置为最后一个参数往往会意外停止交互式终端
  • You need some shiny event to trigger the redrawing of the plot, because d is not a reactive value您需要一些闪亮的事件来触发绘图的重绘,因为d不是反应值
  • If you are on the console you need to assign a new value to d by using the <<- operator because d lives outside the function you are calling:如果您在控制台上,则需要使用<<-运算符为d分配一个新值,因为d位于您正在调用的函数之外:
Browse[2]> d <<- data.frame(x = 1:200, y = 200:1)
  • You can jump out of interactive console and resume the app by entering c and hitting Enter您可以通过输入c并按 Enter 跳出交互式控制台并恢复应用程序

Well, I had the similar doubt!嗯,我也有类似的疑惑! In my case, the solution was to create my own Shiny server: https://github.com/rstudio/shiny-server就我而言,解决方案是创建我自己的 Shiny 服务器: https : //github.com/rstudio/shiny-server

On the one hand, I have my app inside RStudio IDE when I want to change or to test new elements.一方面,当我想更改或测试新元素时,我在 RStudio IDE 中有我的应用程序。 Indeed this is the testing version.确实这是测试版。 To run the application you execute runApp(dir), each time the port changes.要运行应用程序,每次端口更改时都执行 runApp(dir)。

On the other hand, I have the stable version inside the Shiny server.另一方面,我在 Shiny 服务器中有稳定版本。 This is a helpful way to connect from different devices and have a operative version while you are doing some changes.这是一种有用的方法,可以在您进行一些更改时从不同设备进行连接并拥有可操作的版本。 The application is running all of time, you have to configurate your port inside this file: /etc/shiny-server/shiny-server.conf.应用程序一直在运行,你必须在这个文件中配置你的端口:/etc/shiny-server/shiny-server.conf。

If you need more information about Shiny server, consult this website: https://rstudio.github.io/shiny-server/latest/#configuration-settings如果您需要有关 Shiny 服务器的更多信息,请访问此网站: https : //rstudio.github.io/shiny-server/latest/#configuration-settings

Can't you use the <<- global assignment operator?不能使用<<-全局赋值运算符吗? I'm not sure how complicated the variable you need to set is, but say you just need to change some variable t to 5.我不确定您需要设置的变量有多复杂,但假设您只需要将某个变量t更改为 5。

We could make a textBoxInput that changes the input$new_t variable.我们可以创建一个textBoxInput来改变input$new_t变量。 Then have an observer:然后有一个观察者:

observeEvent(input$new_t, t <<- input$new_t)

Then, when input$new_t changes, the global variable t changes.然后,当input$new_t改变时,全局变量t改变。

Edit: Another option is to put a browser() in the object that accesses the variable you need to change, right before the variable is accessed.编辑:另一种选择是在访问您需要更改的变量的对象中放置一个browser() ,就在该变量被访问之前。

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

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