I am trying to find a way to avoid resizing problems without reloading the complete plot when switching tabs in Shiny before the plot is loaded. A minimal example to recreate the problem is given below by drawing samples from a normal distribution magnitude times and plotting a histogram afterwards as a placeholder for a computation-intensive plot.
time_waste<- function(magnitude) {
y<-0
for(i in 1:magnitude) {
y<- y + rnorm(1,0,1)
}
return(abs(y))
}
ui <- fluidPage(sidebarLayout(
sidebarPanel(width = 3,
fluidRow(
column(
4,
numericInput(
inputId = "magnitude",
label = "magnitude",
value = 1000000
)))),
mainPanel(width = 8,
tabsetPanel(id = "tabset",
tabPanel("Plot1", plotlyOutput("p1", height = "700px")),
tabPanel("Plot2", plotlyOutput("p2", height = "700px"))))
)
)
server<- function(input, output, session) {
y<- reactive({
rep(time_waste(time_waste(input$magnitude)),3)
})
output$p1 <- renderPlotly({
p<- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = y(),
name = "SF Zoo",
type = "bar"
)
})
output$p2<- renderPlotly({
p<- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = y(),
name = "SF Zoo",
type = "bar"
)
return(p)
})
}
app <- shinyApp(ui, server)
runApp(app)
The stuck plot then looks like the image linked: Stuck Plot
The plot displays correctly if it gets resized in any way (e.g. by resizing the window it is in) and the problem does not occur with fixed plotwidth.
Kind regards and thanks in advance.