Skip to content

Instantly share code, notes, and snippets.

@johndrummond
Created May 9, 2019 09:13
Show Gist options
  • Save johndrummond/55486a4fab231267b8ef7a0974bc672b to your computer and use it in GitHub Desktop.
Save johndrummond/55486a4fab231267b8ef7a0974bc672b to your computer and use it in GitHub Desktop.
long running task in shiny with task in global future
library(shiny)
library(DT)
library(dplyr)
library(future)
library(promises)
plan(multiprocess)
ui <- fluidPage(
verbatimTextOutput("timer"),
DTOutput("myTable")
)
#time stamp
observe({
invalidateLater(1000, NULL)
print(Sys.time())
})
#initial value
rlist <- reactiveValues()
rlist$tbl_vals <- tibble(Value = rnorm(5))
#set up global observer
observe({
print("observer running")
future ({get_data()}) %>%
then(function(got_data){
isolate({rlist$tbl_vals <<- got_data})
invalidateLater(3000, NULL)
print("now returning data from future")
})
})
server <- function(input, output, session){
#second timer
output$timer <- renderText({
invalidateLater(1000, session)
as.character(Sys.time())
})
output$myTable <- renderDT({
rlist$tbl_vals
})
}
get_data <- function(){
expensive_operation()
tibble(Value = rnorm(20))
}
expensive_operation <- function(){
invisible(rnorm(100000000))
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment