r-lib / callr

Call R from R
https://callr.r-lib.org/
Other
299 stars 38 forks source link

Update progress indicator in sequence #219

Closed dcaud closed 2 years ago

dcaud commented 2 years ago

I've got a nested call to future_lapply that processes in parallel. It works great and fast!

The problem is that reporting on progress in sequence is proving difficult. The following Shiny app has a progress indicator that instead of incrementing like 1,2,3, etc. increments out of order like 1, 5, 2, etc.

Any ideas on how to report progress in order (even if the processing is down out of order)? A simple counter increment doesn't work (increment <- 0; increment <- increment + 1) because of scoping rules.

Switching to plan(multisession) increments in order as expected, but that removes the parallel processing. This is why I'm posting here instead of in the ipc library, but I'm happy to post over there if needed.

library(shiny)
library(future)
library(future.apply)
library(ipc)
library(callr)

plan(list(callr, tweak(multisession, workers = availableCores() -2)))

ui <- fluidPage(
  actionButton("run","Run"),
  tableOutput("dataset")
)

server <- function(input, output, session) {

  dat <- reactiveVal()
  observeEvent(input$run, {
    progress <- AsyncProgress$new(session, min=1, max=15)
    future({
      future.apply::future_lapply(1:15, function(i){
        progress$set(value = i, message = paste("Iteration:",i))
        Sys.sleep(0.5)
      })
      progress$close()
      cars
    }) %...>% dat
    NULL
  })

  output$dataset <- renderTable({
    req(dat())
  })
}

shinyApp(ui, server)
}
gaborcsardi commented 2 years ago

I am not sure why you think this is a callr problem.

dcaud commented 2 years ago

@gaborcsardi I don't think this is necessarily a callr problem. I'm just looking for a solution. Can you suggest a better place to get assistance?

gaborcsardi commented 2 years ago

I am not familiar with these packages, sorry.