theGreatKatzul
theGreatKatzul

Reputation: 447

Load shiny tabPanel in background

I have several tabPanel() blocks in my shiny ui.R that all require a bit of time to load. However, it appears that they are all treated as individual pages and only begin to render once the tabPanel() is select from the navigation bar.

Is there a way to pre-load those pages after the inital tab is loaded in the background?

I have tried to include the following at the bottom of my server function:

sapply(names(outputOptions(output)),function(x) outputOptions(output, x, suspendWhenHidden = FALSE))

This works for the output$variables that live outside of an observer or reactive context, but not for the ones inside.

Response to Victorp

I try to avoid placing renderPlot in an observer call when it makes sense, however I have a reactiveVariable data.frame that is set through a file polling call that then needs to be subset and transformed.

That transformation is used in a series of plots. It was structurally easier to place that reactiveValue in the observer, subset once, and then let all of the resulting plots use that non reactive subset.

  Data = reactivePoll(5479,
               session,
               checkFunc = function() {
                 if (file.exists(orderStatsPath))
                   file.info(orderStatsPath)$mtime[1]
                 else
                   ""
               },
               valueFunc = function() getData(orderStatsPath)
  )

  observe({

    Data = Data()
    Data = subset(Data, subset = Date >= Sys.Date() - 14)
    Data$Date = as.character(Data$Date)
    output$slippageRootTotalUSD = shiny::renderTable({
      result = dcast(Data,
                     Date ~ Symbol,
                     value.var = "slippageRootTotalUSD",
                     na.rm = T,
                     fun = sum,
                     margins = c("Date","Symbol"))

      rownames(result) = result[,1]
      result = signif(result[,-1],5)
      result
    },
    rownames = TRUE,
    hover = TRUE,
    digits = 2,
    spacing = 's'
    )

    output$SizeUSD = shiny::renderTable(
      {
        result = dcast(Data,
                       Date ~ Symbol,
                       value.var = "SizeUSD",
                       na.rm = T,
                       fun = sum,
                       margins = c("Date","Symbol"))

        rownames(result) = result[,1]
        result = round(result[,-1],0)

        result
      },
      rownames = TRUE,
      hover = TRUE,
      digits = 0,
      spacing = 's'
    )
    sapply(names(outputOptions(output)),function(x) outputOptions(output, x, suspendWhenHidden = FALSE))
 }

Upvotes: 2

Views: 1387

Answers (1)

Victorp
Victorp

Reputation: 13856

Look at ?outputOptions, you probably want to use that :

# Disable suspend for output$myplot
outputOptions(output, "myplot", suspendWhenHidden = FALSE)

Upvotes: 6

Related Questions