2

There is a similar question here, but I haven't found a satisfactory answer.

When I output two plot, one is interactive using plotly and one is static figure using R package pheatmep. The static image will not show in shiny page, but it shows in Studio panel sometimes.

  1. When I comment out the plotly output, the static figure produced from pheatmap can properly appear in shiny.

  2. When I convert the plotly interactive figure static, both two figures can be properly displayed in shiny.

I don't know the problem is because of Shiny, or pheatmap, or RStudio, or plotly. Could someone please help?

Below is an example to describe the problem.

library(shiny)
library(plotly)
library(ggfortify)
library(pheatmap)

ui <- fluidPage(
  actionButton(inputId = "viewPlot", label = "View"),
  plotly::plotlyOutput("plot1", height = 250),
  shiny::plotOutput("plot2", height = 250)
)

server <- function(input, output) {

  df <- iris[1:4]
  pca_res <- prcomp(df, scale. = TRUE)

  p1 <- reactive({
    ggplotly(autoplot(pca_res, data = iris, colour = 'Species'))
  })

  p2 <- reactive({
    return(pheatmap(scale(df)))
  })

  observeEvent(input$viewPlot, {
    output$plot1 <- renderPlotly({
      p1()
    })

    output$plot2 <- renderPlot({
      p2()
    })
  })
}

shinyApp(ui, server)


@ismirsehregal edit:

Here is why I don't think this is related:

library(shiny)
library(plotly)
library(ggfortify)
library(pheatmap)

ui <- fluidPage(
  p(),
  actionButton(inputId = "viewPlot", label = "View"),
  plotly::plotlyOutput("plot1", height = "30vh"),
  shiny::plotOutput("plot2", height = "30vh"),
  shiny::plotOutput("plot3", height = "30vh")
)

pca_res <- prcomp(DF, scale. = TRUE)

server <- function(input, output, session) {
  reactiveDF <- eventReactive(input$viewPlot, {
    iris
  })
  
  output$plot1 <- renderPlotly({
    ggplotly(autoplot(pca_res, data = reactiveDF(), colour = 'Species'))
  }) # %>% bindEvent(input$viewPlot) # alternative to eventReactive
  
  output$plot2 <- renderPlot({
    pheatmap(scale(reactiveDF()[1:4]))
  })
  
  output$plot3 <- renderPlot({
    plot(x = seq_len(nrow(reactiveDF())), y = reactiveDF()$`Sepal.Length`)
  })
}

shinyApp(ui, server)

result

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
Wang
  • 1,314
  • 14
  • 21
  • 1
    Indeed this is a strange behavior. I also notice that if I open the app in the browser, after clicking on the button only the plotly plot appears but if I run Ctrl+Shift+C to see the Inspector then the heatmap also appears – bretauv Feb 24 '22 at 09:13
  • 1
    Ah it's because opening the inspector reduces the width of the page on my browser. It seems that when my browser window is maximized, the plot doesn't appear but if I resize it to make it less wide then the plot appears. That's really strange – bretauv Feb 24 '22 at 09:16
  • 1
    However, I don't think this is `plotly` related. The `pheatmap` behaviour seems buggy. Btw. you should never nest `render*` functions in `observeEvent`. – ismirsehregal Feb 24 '22 at 09:22
  • @ismirsehregal I think it is related to `plotly`. With `plotlyOutput` before `plotOutput`, the heatmap is not rendered. If you comment `plotlyOutput`, the heatmap is rendered. If you replace `plotlyOutput` by another `plotOutput` or by `echarts4rOutput`, the heatmap is also rendered. – bretauv Feb 24 '22 at 09:36
  • 1
    @bretauv commenting out the plotly part doesn't change the behaviour on my system. `pheatmap` needs a resize event to render. – ismirsehregal Feb 24 '22 at 09:44
  • @bretauv please see my edit. – ismirsehregal Feb 24 '22 at 10:12
  • 1
    @Wang you might want to file an issue [here](https://github.com/raivokolde/pheatmap/issues). However the latest commit over there is from end of 2018. Are you aware of library([heatmaply](https://github.com/talgalili/heatmaply))? – ismirsehregal Feb 24 '22 at 10:31
  • @ismirsehregal, Thanks a lot. You are amazing!!! I have tried with heatmaply, it worked very well. But the heatmap produced by `heatmaply` is not as nice as `pheatmap`. – Wang Feb 24 '22 at 10:56
  • 1
    @Wang - you are welcome but it's just a workaround - maybe you are able to tune `heatmaply`'s appearance to your needs? - also check [plotly's heatmaps](https://plotly.com/r/heatmaps/). – ismirsehregal Feb 24 '22 at 11:02
  • [Here](https://stackoverflow.com/questions/60317281/heatmap-is-not-rendered-in-shiny-unless-i-resize-the-window) is a related question. – ismirsehregal Feb 24 '22 at 20:05
  • 1
    FYI: I filed an issue [here](https://github.com/raivokolde/pheatmap/issues/85). – ismirsehregal Mar 11 '22 at 22:27
  • 1
    I've had the same problem and found a workaround by using `as.ggplot(pheatmap(...))` from `ggplotify`. The graph looks identical after conversion. – Rasmus Apr 12 '22 at 20:20
  • @Rasmus, thanks a lot. I would love to try it. – Wang Apr 13 '22 at 07:22
  • @Rasmus, The problem still remains after converting `pheatmap` to `ggplot2` type image. It didn't seem to work. – Wang Apr 13 '22 at 08:31
  • Sorry, yes I also noticed this. Happens so infrequently so I thought it solved it :( – Rasmus Apr 19 '22 at 15:03

0 Answers0