ropensci / iheatmapr

Complex, interactive heatmaps in R
https://docs.ropensci.org/iheatmapr
Other
267 stars 35 forks source link

Bug: error causes heatmap to disappear if it tries to re-plot after a plotly plot is rendered #83

Open daattali opened 3 years ago

daattali commented 3 years ago

This is a fatal bug but it seems to occur only in the following sequence:

  1. heatmap is displayed before any plotly plots have rendered
  2. a plotly plot is rendered (by going to a different tab)
  3. heatmap attempts to re-render based on a reactive value
  4. javascript error causes heatmap to not render properly

To reproduce, use the following code, which plots a heatmap using code from the docs in one tab and a plotly plot in another tab:

library(iheatmapr)
library(shiny)

utils::data(measles, package = "iheatmapr")

ui <- fluidPage(
  tabsetPanel(
      tabPanel(
          "heatmap",
          selectInput("legend", "legend title", LETTERS),
          iheatmapr::iheatmaprOutput("heatmap")
      ),
      tabPanel(
          "plotly",
          plotly::plotlyOutput("plotly")
      )
  )
)

server <- function(input, output, session) {
  output$heatmap <- iheatmapr::renderIheatmap({
      main_heatmap(measles, name = input$legend, x_categorical = FALSE,
                   layout = list(font = list(size = 8))) %>%
          add_col_groups(ifelse(1930:2001 < 1961,"No","Yes"),
                         side = "bottom", name = "Vaccine<br>Introduced?",
                         title = "Vaccine?",
                         colors = c("lightgray","blue")) %>%
          add_col_labels(ticktext = seq(1930,2000,10),font = list(size = 8)) %>%
          add_row_labels(size = 0.3,font = list(size = 6)) %>%
          add_col_summary(layout = list(title = "Average<br>across<br>states"),
                          yname = "summary")  %>%
          add_col_title("Measles Cases from 1930 to 2001", side= "top") %>%
          add_row_summary(groups = TRUE,
                          type = "bar",
                          layout = list(title = "Average<br>per<br>year",
                                        font = list(size = 8)))
  })

  output$plotly <- plotly::renderPlotly({
      plotly::plot_ly(cars, x = ~dist)
  })
}

shinyApp(ui, server)

Before going to the plotly tab, when you change the dropdown input, the heatmap updates. After going to the plotly tab and then back to the heatmap tab, when you change the input the heatmap breaks

image

Notes for troubleshooting:

  1. This only happens with plotly. If you change the plotly plot to ggplot2, there are no errors
  2. If a plotly plot is rendered before the heatmap, this problem won't happen. To see that, swap the two tabs or add another plotly plot to the first tab
  3. The following javascript error is printed to the console which might give clues to the problem:
Uncaught TypeError: Cannot read property 'undefined' of undefined
    at e.exports (<anonymous>:61:477869)
    at Object.x.supplyDefaults (<anonymous>:61:845823)
    at Object.r.plot (<anonymous>:61:551064)
    at Object.r.newPlot (<anonymous>:61:550427)
    at Object.renderValue (iheatmapr.js:34)
    at exports.OutputBinding.shinyBinding.renderValue (htmlwidgets.js:541)
    at exports.OutputBinding.onValueChange (output_binding.js:16)
    at exports.OutputBinding.delegator.<computed> [as onValueChange] (htmlwidgets.js:112)
    at OutputBindingAdapter.onValueChange (output_binding_adapter.js:21)
    at ShinyApp.receiveOutput (shinyapp.js:332)
BursacPetar commented 2 years ago

Hi @daattali @AliciaSchep Please let me know whether anything has been done about this problem? I just have an identical issue and I wouldn’t want to move on static ggplot. Thanks, Kind regards, Petar