shosaco
shosaco

Reputation: 6165

R/Shiny: Plotly reactive height discarded after window resize

After upgrade from Plotly 4.5.6 to Plotly 4.6.0 (or higher), the plot height is incorrectly calculated when set reactively in plot_ly. This happens upon window resizing or tabsetPanel selection.

Prerequisites

Problem

Example

ex1

ex2

The same problem happens if there is a tabsetPanel in the page (e.g. below the plot). The plot height is changed when a different tab is clicked.

library(shiny)
library(plotly)


ui <-shinyUI(fluidPage(
       selectInput("height", "Choose desired height", choices=c(100,800)), 
       plotlyOutput("plot")))

server <- shinyServer(function(input,output, session){
  output$plot <- renderPlotly({
    plot_ly(mtcars, x=~gear, y=~cyl, height = input$height)
  })
})

shinyApp(ui,server)

What I tried so far

  1. Putting the height argument in renderPlotly works initially, but since the Plotly itself does never change its height, the plot itself stays at 800px the whole time (just the container height is changed):

ui: uiOutput("plot")

server:

output$plot <- renderUI({ plotlyOutput("plotly", height=input$height) })  
output$plotly <- renderPlotly({ plot_ly(mtcars, x=~gear, y=~cyl)})
  1. When I additionally make output$plotly dependent on input$height, the Plots "jumps" immediately after selecting any other height:

output$plotly <- renderPlotly({ plot_ly(mtcars, x=~gear, y=~cyl, height=input$height) })

Note that all of this also happens if there is a simple tabsetPanel below the plot and another tab is chosen there.

Upvotes: 5

Views: 1165

Answers (1)

shosaco
shosaco

Reputation: 6165

This has been fixed finally, see this bug on github:. In plotly package version 4.8.0 this behaviour is corrected.

Note that you need to add the height of 100% to plotlyOutput, i.e. plotlyOutput("plot", height = '100%') for a complete solution (this sets the invisible container space appropriately).

Upvotes: 3

Related Questions