R A brilliant task to run / script in another process

In my brilliant app, users can generate a heavy PowerPoint report. When there will be many slides in it, it may take> 30 minutes. And so I would like to handle these tasks in independent processes / tasks that could work even when the application is closed - for example. the user presses a button to create a report, closes the application and when the application is ready to be sent, informs the user by e-mail. Is there any good practice or proven solutions for this?

My first thought was to use the package futurewith a set plan(multisession)- but I'm not sure what will happen when the user closes the application - the session futureis closed or not?

+6
source share
2 answers

I was fortunate to be at the London EARL this week, and I think one of the best presentations I saw there was in this one (Joe Chen). To do this, you will need the promises package , and, as the documentation says, a special version of the brilliant devtools::install_github("rstudio/shiny@async")that supports asynchronous programming.

Here you can find the first documentation on how this works with dplyrand promises( futurealso compatible),

As a small example (taken from the documentation), an intensive calculation is performed using the following:

read.csv.async("data.csv") %...>%
  filter(state == "NY") %...>%
  arrange(median_income) %...>%
  head(10) %...>%
  View()

, , View, . , , ( , ).

+8

, future. (), . , - , , . ?

library(future)
cl <- parallel::makeCluster(2L)
plan(cluster, workers = cl)

server <- function(input, output) {
  observeEvent(input$run, {

    iteration <- as.numeric(input$iteration)
    path <- input$path

    future::future({
      writeLog <- function(n, path) {
        file.remove(path)
        for (i in 1:n) {
          cat("#", i, "-",  as.character(Sys.time()), "\n", file = path, append = TRUE)
          Sys.sleep(1)
        }
      }
      writeLog(iteration, path)
    }, globals = c("iteration", "path"))
  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      tags$div("This app writes to file in cluster which means it is computed in parallel to this session. 
               It will execute even when app is closed.")
      , br()
      , shiny::textInput("path", "Path to log file", value = "/src/dev/export_performance/future.log")
      , shiny::textInput("iteration", "Iteration number", value = 60)    
    ),
    mainPanel(
      br()
      , actionButton("run", "Run future")
    )
  )
)

shinyApp(ui = ui, server = server)
0

All Articles