I was wondering if anyone could help with how the htmlwidgets and associated render functions work with promises (i.e promises
and future
packages) in shiny
.
I have been reading up and learning on how to integrate these things based on Joe Chengs articles and rstudioconf talk. E.g
https://medium.com/@joe.cheng/async-programming-in-r-and-shiny-ebe8c5010790 https://medium.com/@joe.cheng/an-informal-intro-to-async-shiny-cbf01c85c4c5
https://www.rstudio.com/resources/videos/scaling-shiny-apps-with-async-programming/ (nice talk by the way)
An issue was raised on the github repo for the promises
package concerning DT but it was stated that most htmlwidget packages will not need modification.
https://github.com/rstudio/promises/issues/10
I have tried numerous things to get htmlwidgets to render from promises but can't get this working with shiny. For example with dygraphs
and leaflet
the following works outside of shiny (A standard plot()
is also included as a reference)
library(promises)
library(future)
plan(multiprocess)
library(shiny)
library(leaflet)
library(dygraphs)
expensive_operation = function(){
Sys.sleep(2)
data.frame(x=runif(10))
}
future({ expensive_operation() }) %...>% plot
future({ expensive_operation() }) %...>%
{leaflet(.) %>% addTiles() %>% addMarkers(~x,~x) %>% print}
future({ expensive_operation()}) %...>% {
dygraph(.) %>% print
}
All fine so far.
I have installed the dev version of shiny
from github. But in the following:
ui = fluidPage(
leafletOutput("l"),
plotOutput("p"),
dygraphOutput("d")
)
server = function(input, output, session) {
output$p = renderPlot({
future({ expensive_operation() }) %...>% plot
})
output$l <- renderLeaflet({
## neither
# future({ expensive_operation() }) %...>%
# {leaflet(.) %>% addTiles() %>% addMarkers(~x,~x) %>% print} # prints in rstudio viewer
# nor
future({ expensive_operation() }) %...>%
{leaflet(.) %>% addTiles()%>% addMarkers(~x,~x)} # does nothing
# work
})
#similar story to leaflet
output$d = renderDygraph({
# future({ expensive_operation()}) %...>% {
# dygraph(.) %>% print
# }
future({ expensive_operation()}) %...>% {
dygraph(.)
}
})
}
runApp(shinyApp(ui,server))
only the standard plot actually renders. Neither the commented out version with a call to print
or the other will do anything inside the shiny app. Including print
does allow this to be printed to the rstudio viewer but this isn't fit for purpose.
Does anyone have any suggestions as to where I am misunderstanding or going wrong?