John
John

Reputation: 1828

R shiny tab sets simultaneous processing

In my R shiny app, I have many tabPanels in my tabsetPanel.

The charts of a specific tab won't begin to load until I click that tab.

So it takes a long time to just go through the contents of all tabs.

Is there any way to let all tabs process first when the app is launched so all the charts are already there when I go to different tabs?

I created a simple example with two histograms:

  server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(100000000), col = 'darkgray', border = 'white')
  })


  output$distPlot2 <- renderPlot({
    hist(rnorm(100000000), col = 'red', border = 'white')
  })
  outputOptions(output,"distPlot2",suspendWhenHidden = FALSE)

}

ui <- fluidPage(

  tabsetPanel(
    tabPanel("1",plotOutput("distPlot")
      ),
    tabPanel("2",plotOutput("distPlot2")
      )
    )

)

shinyApp(ui = ui, server = server)

I timed the loading of these two histgrams and found that the option suspendWhenHidden = FALSE is not working here. How to fix it?

Upvotes: 1

Views: 429

Answers (1)

zero323
zero323

Reputation: 330163

You can use suspendWhenHidden parameter for shiny::outputOptions to control rendering behavior:

suspendWhenHidden. When ‘TRUE’ (the default), the output object will be suspended (not execute) when it is hidden on the web page. When ‘FALSE’, the output object will not suspend when hidden, and if it was already hidden and suspended, then it will resume immediately.

If that's not enough you can execute expensive part of you code either when application starts (outside server function), or per user (in server outside render blocks).

Upvotes: 1

Related Questions