HenrikBengtsson / progressr

三 R package: An Inclusive, Unifying API for Progress Updates
https://progressr.futureverse.org
280 stars 12 forks source link

difference between operating Systems in a multisession shiny progressor #164

Open wstvcg opened 10 months ago

wstvcg commented 10 months ago

The following minimal code seems to works flawlessly with handlers("progress") both in linux and windows.

handler_shiny only seems to work in parallel with a windows os. The linux shiny app only shows half of the progress steps in a sequential order.

Any advice?

 library(shiny)
  library(progressr)
  library(purrr)
  library(furrr)
  library(tibble)

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  my_fct <- function(x,
                     workers = 2
  ) {

    my_steps <- nrow(dplyr::bind_rows(x))
    p <- progressor(steps = my_steps * workers)
    set.seed(123)
    future::plan(multisession, workers = workers)

    x |>
      furrr::future_walk(\(list) {
        list[[1]] |>
          purrr::walk(\(item) {
            Sys.sleep(.25)
            p(message = item)
          })

      })
  }
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  my_list <- list(one = tibble(a = as.character(1:20)),
                  two = tibble(a = letters[1:20]))
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #future::plan(multisession, workers = 2)
  handlers("progress")
  system.time(progressr::with_progress(my_list |> my_fct()))
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  shinyApp(
    ui = fluidPage(plotOutput("plot")),

    server = function(input, output) {
      output$plot <- renderPlot({

        progressr::withProgressShiny(
          message = "Calculation in progress",
          detail = "Starting ...",
          value = 0,

          handlers = c(shiny = handler_shiny,
                       progressr::handlers(default = 'progress')),
          {
            result <- my_list |>
              my_fct()
          plot(cars)
          }

        )
      })

    }
  )
HenrikBengtsson commented 10 months ago

The linux shiny app only shows half of the progress steps in a sequential order.

It's not clear to me if "half of ...", means the "first half" or "every second".

There should be nothing causing a difference between Linux and MS Windows when using 'multisession' for parallelization.

I adopted the example("withProgressShiny", package = "progressr") example to use future_lapply() instead of lapply(), set plan(multisession, workers = 2) and extend the delay from 0.25 to 2.0 seconds;

library(shiny)
library(progressr)
library(future.apply)
plan(multisession, workers = 2)

app <- shinyApp(
  ui = fluidPage(
    plotOutput("plot")
  ),

  server = function(input, output) {
    output$plot <- renderPlot({
      X <- 1:15
      withProgressShiny(message = "Calculation in progress",
                        detail = "Starting ...",
                        value = 0, {
        p <- progressor(along = X)
        y <- future_lapply(X, FUN=function(x) {
          Sys.sleep(2.0)
          p(sprintf("x=%d", x))
        })
      })

      plot(cars)

      ## Terminate the Shiny app
      Sys.sleep(1.0)
      stopApp(returnValue = invisible())
    })
  }
)

local({
  oopts <- options(device.ask.default = FALSE)
  on.exit(options(oopts))
  if (interactive()) print(app)
})

When running this on Ubuntu 22.04 Linux with R 4.3.2, I clearly see 'x = 1' quickly followed by 'x = 8', then a bit of wait before 'x = 2' and 'x = 9' are reported. This continues with all numbers until 'x = 15' completes it.

Do you see something different?

> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS

Matrix products: default
BLAS:   /home/henrik/shared/software/CBI/_ubuntu22_04/R-4.3.2-gcc11/lib/R/lib/libRblas.so 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/Los_Angeles
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] future.apply_1.11.0 future_1.33.0-9004  progressr_0.14.0   
[4] shiny_1.8.0        

loaded via a namespace (and not attached):
 [1] cli_3.6.2         rlang_1.1.2       promises_1.2.1    textshaping_0.3.7
 [5] jsonlite_1.8.8    xtable_1.8-6      listenv_0.9.0     htmltools_0.5.7  
 [9] httpuv_1.6.13     ragg_1.2.7        sass_0.4.8        jquerylib_0.1.4  
[13] ellipsis_0.3.2    fastmap_1.1.1     lifecycle_1.0.4   memoise_2.0.1    
[17] compiler_4.3.2    codetools_0.2-19  Rcpp_1.0.11       later_1.3.2      
[21] systemfonts_1.0.5 digest_0.6.33     R6_2.5.1          parallelly_1.36.0
[25] parallel_4.3.2    magrittr_2.0.3    bslib_0.6.1       tools_4.3.2      
[29] mime_0.12         globals_0.16.2    cachem_1.0.8