Async Programming in Shiny

I am working on a Shiny application which will be used by multiple users and involves expensive operations (pulling data from APIs). Since I am currently testing the application, I use the following function to toggle between whether async programming is to be used or not:

MDIS_ASYNC <- TRUE

if (MDIS_ASYNC) {
  library(promises)
  library(future)
  plan(multiprocess)
  future <- future::future
  then <- promises::then
} else {
  future <- base::identity
  then <- function(promise, onFulfilled) { onFulfilled(promise) }
}

I am then trying to render a data table using the following code:

fetch_data_from_stratum <- reactive({
  req(ShipId())
  req(startperiod())
  req(stopperiod())
  id <- ShipId()$VesselID[1]
  strtp <- as.character(startperiod())
  stpp <- as.character(stopperiod())
  shinyjs::show("page_shield")
  future({
    list(
      data=fetch_voyage_data(id,strtp,stpp)
    )
  })
})
#
voyData <- reactive({
  then(
    fetch_data_from_stratum(),
    onFulfilled = function(value){
      shinyjs::hide("page_shield")
      value$data
    }
  )
})
output$voyageData <- renderDT({
  req(voyData())
  then(
    voyData(),
    onFulfilled = function(value){
      datatable(value)
    }
  )
})

am able to render voyageData DT when I am not using async programming ( MDIS_ASYNC <- FALSE ). But when it is true, I get the following error:

Warning: Error in datatable: 'data' must be 2-dimensional (e.g. data frame or matrix)

I have just installed the latest Github version of DT.
Can someone help here please?

Author of future here: This might be a case where the automatic identification of globals and packages fails to identify code dependency on the data.table package. Section ' Missing packages (false negatives)' in vignette A Future for R: Common Issues with Solutions illustrates this problem.Try using:

future({
  ...
}, package = "data.table")

to make sure data.table is attached in the worker.

1 Like

@HenrikBengtsson thanks, will give this a shot.