我正在开发一个Shiny应用程序,该应用程序将被多个用户使用,并且涉及昂贵的操作(从API中提取数据)。由于我目前正在测试该应用程序,因此我使用以下函数在是否要使用异步编程之间切换:
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) }
}
然后我尝试使用以下代码呈现数据表:
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)
}
)
})
当我不使用异步编程(voyageData
)时,我可以渲染MDIS_ASYNC <- FALSE
DT。但是,当它为真时,会出现以下错误:
List of 3
$ then :function (onFulfilled = NULL, onRejected = NULL)
$ catch :function (onRejected)
$ finally:function (onFinally)
- attr(*, "class")= chr "promise"
- attr(*, "promise_impl")=Classes 'Promise', 'R6' <Promise [pending]>
Warning: Error in datatable: 'data' must be 2-dimensional (e.g. data frame or matrix)
有人可以帮忙吗?