Created
May 8, 2019 23:13
-
-
Save johndrummond/ec0c55a4161a5aaa3df9cc7826fa907a to your computer and use it in GitHub Desktop.
shiny app with long running calculation using promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# notice the flush on the single user holds up the user updating, as the promise and reactive variable is there per user | |
library(shiny) | |
library(DT) | |
library(dplyr) | |
library(future) | |
library(promises) | |
plan(multiprocess) | |
ui <- fluidPage( | |
verbatimTextOutput("timer"), | |
DTOutput("myTable") | |
) | |
observe({ | |
invalidateLater(1000, NULL) | |
print(Sys.time()) | |
}) | |
server <- function(input, output, session){ | |
#initial value | |
rlist <- reactiveValues() | |
rlist$tbl_vals <- tibble(Value = rnorm(5)) | |
#second timer | |
output$timer <- renderText({ | |
invalidateLater(1000, session) | |
as.character(Sys.time()) | |
}) | |
observe({ | |
print("observer running") | |
future ({get_data()}) %>% | |
then(function(got_data){ | |
isolate({rlist$tbl_vals <<- got_data}) | |
invalidateLater(3000, session) | |
print("now returning data from future") | |
}) | |
}) | |
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