【问题标题】:Shiny plot shows up in RStudio window instead of in browser闪亮的绘图显示在 RStudio 窗口中,而不是在浏览器中
【发布时间】:2019-08-20 14:42:56
【问题描述】:

如果您在 RStudio 中运行以下代码,它会正确显示两个绘图,但如果您进行小改动(详情如下),它将不再显示第二个绘图。

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

library(fgsea)
library(dplyr)
library(ggplot2)
library(shiny)

ui <- fluidPage(
  plotOutput("gseaPlot"),
  plotOutput("gseaEnrichment")
)

runAnalysis <- function() {
  gseaResult <- fgsea(pathways = examplePathways, stats = exampleRanks, nperm = 10)
  topPathways <- gseaResult[NES > 0][head(order(desc(NES)), n = 10), pathway]
  topPathwayUp <- topPathways[[1]]
  gseaEnrichment <- plotEnrichment(examplePathways[[topPathwayUp]], exampleRanks)
  gseaPlot <- plotGseaTable(examplePathways[topPathways], exampleRanks, gseaResult)
  list(gseaEnrichment = gseaEnrichment, gseaPlot = gseaPlot)
}

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

shinyApp(ui = ui, server = server)

小变化是,如果我将第二个渲染图更改为使用theAnalysis 而不是runAnalysis(),如下所示:

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

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

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

更新:实际上,以下显示的问题更简单,不涉及 Shiny。如果我运行以下代码,该图会显示在 RStudio 绘图查看器中,即使它已存储到变量中(但 plotEnrichment 并非如此):

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)

更新:显然 fgsea 1.6 版不会发生这种情况(我使用的是 1.8 版),所以它可能是 fgsea 中的一个错误,所以我提交了一个 fgsea 问题。但如果有人能看到问题或有解决方法,我欢迎回答。

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    这是因为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)
      })
    }
    

    向 fgsea 提出了一个拉取请求,以允许 plotGseaTable:https://github.com/ctlab/fgsea/pull/43

    【讨论】:

      猜你喜欢
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 2018-05-21
      • 2019-05-24
      • 1970-01-01
      相关资源
      最近更新 更多