闪亮的绘图显示在RStudio窗口中,而不是浏览器中

闪亮的绘图显示在RStudio窗口中,而不是浏览器中,r,ggplot2,shiny,R,Ggplot2,Shiny,如果您在RStudio中运行以下代码,它将正确显示两个绘图,但如果您做了一个小更改(如下所述),它将不再显示第二个绘图 (请注意,examplePathways和exampleRanks由fgsea包提供,因此以下代码应该可以按原样运行。) 然后,第二个绘图突然出现在RStudio查看器窗口中,而不是浏览器中 这里发生了什么导致这种行为?我如何在不运行两次runAnalysis()的情况下修复它 更新:事实上,下面的内容更简单地显示了问题,并且不涉及任何问题。如果我运行以下代码,则该绘图将显示在

如果您在RStudio中运行以下代码,它将正确显示两个绘图,但如果您做了一个小更改(如下所述),它将不再显示第二个绘图

(请注意,
examplePathways
exampleRanks
由fgsea包提供,因此以下代码应该可以按原样运行。)

然后,第二个绘图突然出现在RStudio查看器窗口中,而不是浏览器中

这里发生了什么导致这种行为?我如何在不运行两次
runAnalysis()
的情况下修复它

更新:事实上,下面的内容更简单地显示了问题,并且不涉及任何问题。如果我运行以下代码,则该绘图将显示在RStudio绘图查看器中,即使它已存储到变量中(但PlotEnrich的情况并非如此):

库(dplyr)
图书馆(fgsea)
图书馆(GG2)

gseaResult这是由于
plotGseaTable
正在调用
grid.arrange
,从而呈现到当前设备。这就是为什么直接从反应性上下文运行它是有效的,而在反应性上下文之外运行它则不是

解决方案是让
plotGseaTable
返回grob,然后在反应式上下文中进行渲染,如下所示:

library(grid)

...

runAnalysis <- function() {
  ...
  # The render = FALSE here (not yet available in fgsea)
  # which allows the plot to be rendered later
  gseaPlot <- plotGseaTable(examplePathways[topPathways], exampleRanks, gseaResult, render = FALSE)
  ...
}

server <- function(input, output, session) {
  theAnalysis <- runAnalysis()
  output$gseaEnrichment <- renderPlot({
    theAnalysis$gseaEnrichment
  })
  output$gseaPlot <- renderPlot({
    grid.draw(theAnalysis$gseaPlot)
  })
}
库(网格)
...

运行分析这是由于
plotGseaTable
正在调用
grid.arrange
,从而呈现到当前设备。这就是为什么直接从反应性上下文运行它是有效的,而在反应性上下文之外运行它则不是

解决方案是让
plotGseaTable
返回grob,然后在反应式上下文中进行渲染,如下所示:

library(grid)

...

runAnalysis <- function() {
  ...
  # The render = FALSE here (not yet available in fgsea)
  # which allows the plot to be rendered later
  gseaPlot <- plotGseaTable(examplePathways[topPathways], exampleRanks, gseaResult, render = FALSE)
  ...
}

server <- function(input, output, session) {
  theAnalysis <- runAnalysis()
  output$gseaEnrichment <- renderPlot({
    theAnalysis$gseaEnrichment
  })
  output$gseaPlot <- renderPlot({
    grid.draw(theAnalysis$gseaPlot)
  })
}
库(网格)
...
运行分析
library(dplyr)
library(fgsea)
library(ggplot2)
gseaResult <- fgsea(pathways = examplePathways, stats = exampleRanks, nperm = 10)
topPathways <- gseaResult[NES > 0][head(order(desc(NES)), n = 10), pathway]
gseaPlot <- plotGseaTable(examplePathways[topPathways], exampleRanks, gseaResult)
library(grid)

...

runAnalysis <- function() {
  ...
  # The render = FALSE here (not yet available in fgsea)
  # which allows the plot to be rendered later
  gseaPlot <- plotGseaTable(examplePathways[topPathways], exampleRanks, gseaResult, render = FALSE)
  ...
}

server <- function(input, output, session) {
  theAnalysis <- runAnalysis()
  output$gseaEnrichment <- renderPlot({
    theAnalysis$gseaEnrichment
  })
  output$gseaPlot <- renderPlot({
    grid.draw(theAnalysis$gseaPlot)
  })
}