简体   繁体   English

闪亮的应用程序中的ggplot转到rstudio情节窗口

[英]ggplot in shiny app go to rstudio plot window

I have a shiny app that draw some ggplot2 plots in app. 我有一个闪亮的应用程序,在应用程序中绘制一些ggplot2图。 Now I'm making it into a package and export the plot drawing as a function. 现在我将它变成一个包并将绘图绘图导出为一个函数。 I found once I draw some ggplot in RStudio and start my shiny app, then all plots in my shiny app went to the RStudio plot pane. 我发现一旦我在RStudio中绘制一些ggplot并启动我的闪亮应用程序,然后我闪亮的应用程序中的所有情节都转到了RStudio情节窗格。

I have tracked down the problem to very specific location and made a minimal working example. 我已经将问题追踪到非常具体的位置,并做了一个最小的工作示例。

This shiny app draw a ggplot, though it first save the ggplot into png with ggsave, then return the ggplot object to renderPlot so that it is shown in app. 这个闪亮的应用程序绘制一个ggplot,虽然它首先将ggplot保存到png与ggsave,然后将ggplot对象返回到renderPlot,以便它显示在app中。

library(shiny)
library(ggplot2)
SAVE_PLOT <- TRUE
ui <- fluidPage(fluidRow(column(12, plotOutput("plot"))))
server <- function(input, output){
  output$plot <- renderPlot({
    g <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
    if (SAVE_PLOT) {
      ggsave("plot.png", g)
    }
    g
  })
}
shinyApp(ui = ui, server = server)

If I run this simple code in RStudio first 如果我先在RStudio中运行这个简单的代码

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) + geom_point()

then run the app above, the plot in app is shown in RStudio plot window. 然后运行上面的应用程序,应用程序中的绘图显示在RStudio绘图窗口中。

If I set SAVE_PLOT <- FALSE then the app will have plot shown correctly. 如果我设置SAVE_PLOT < - FALSE,则应用程序将正确显示绘图。 This show the problem is caused by ggsave. 这表明问题是由ggsave引起的。

I didn't try if plot first and save it later will not have this problem, because the plot in my real app is reactive, plot it first means I need to wrap every plot into a reactive instead of just write in renderPlot. 我没有先尝试绘图并稍后保存它不会有这个问题,因为我的真实应用程序中的绘图是反应性的,首先绘制它意味着我需要将每个绘图包装成一个被动反应而不是仅仅在renderPlot中写入。

If I run dev.off after the ggplot in console, the plot will not go to rstudio. 如果我在控制台中的ggplot之后运行dev.off,则情节不会转到rstudio。 Though this is obviously not a solution either. 虽然这显然不是解决方案。

According to ggsave code , it create a new device then turn off current device after saving. 根据ggsave代码 ,它会创建一个新设备,然后在保存后关闭当前设备。 Is it possible that it actually messed up in this case and turned off the shiny plot device also? 它是否有可能在这种情况下搞砸了并关闭了闪亮的绘图设备呢?

It's also possible to be something with Shiny, or with RStudio. 它也可能是Shiny或RStudio的东西。

UPDATE: the problem is not specific to RStudio, the R console have same problem. 更新:问题不是特定于RStudio,R控制台有同样的问题。

Thanks for hints from @Claus Wilke, I don't think we should turn off the current device but we can save the device and restore it. 感谢@Claus Wilke的提示,我认为我们不应该关闭当前的设备,但我们可以保存设备并恢复它。

We can use this to work around the problem: 我们可以用它来解决这个问题:

library(shiny)
library(ggplot2)
SAVE_PLOT <- TRUE
ui <- fluidPage(fluidRow(column(12, plotOutput("plot"))))
server <- function(input, output){
  output$plot <- renderPlot({
    g <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
    if (SAVE_PLOT) {
      cur_dev <- dev.cur()
      ggsave("plot.png", g)
      dev.set(cur_dev)
    }
    g
  })
}
shinyApp(ui = ui, server = server)

Thanks to @wch, issue has been created in ggplot2 repo since it's a ggsave bug. 感谢@wch,问题是在ggplot2 repo中创建的,因为它是一个ggsave错误。

Enclosing the ggsave() function in pdf(NULL); ggsave(...); dev.off() ggsave() pdf(NULL); ggsave(...); dev.off()ggsave()函数ggsave() pdf(NULL); ggsave(...); dev.off() pdf(NULL); ggsave(...); dev.off() pdf(NULL); ggsave(...); dev.off() seems to provide a workaround here, though sometimes the graphics device is in strange state when closing the app. pdf(NULL); ggsave(...); dev.off()似乎提供了一种解决方法,但有时候关闭应用程序时图形设备处于奇怪的状态。

library(shiny)
library(ggplot2)
SAVE_PLOT <- TRUE
ui <- fluidPage(fluidRow(column(12, plotOutput("plot"))))
server <- function(input, output){
  output$plot <- renderPlot({
    g <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
    if (SAVE_PLOT) {
      pdf(NULL)
      ggsave("plot.png", g)
      dev.off()
    }
    g
  })
}
qplot(1:10, 1:10) # shows up in R Studio window
shinyApp(ui = ui, server = server) # shows up in shiny window

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

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