'R shiny: plotlyOutput and plotOutput conflict

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source