Skip to content

stopMulticoreFuture Example Fails #27

@karchjd

Description

@karchjd

Hello, for me, the stopMulticoreFuture Example from the documentation fails. If I press cancel, the process does not stop and I get error Warning: Error in stopMulticoreFuture: stopMulticoreFuture only works on multicore futures and the app crashes after the 10 seconds wait.

Here is my sessionInfo output:

R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.1.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Amsterdam
tzcode source: internal

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

other attached packages:
[1] promises_1.3.0 future_1.34.0  ipc_0.1.4      shiny_1.9.1   

loaded via a namespace (and not attached):
 [1] crayon_1.5.3      cli_3.6.3         rlang_1.1.4       jsonlite_1.8.9    xtable_1.8-4      listenv_0.9.1    
 [7] backports_1.5.0   htmltools_0.5.8.1 httpuv_1.6.15     sass_0.4.9        jquerylib_0.1.4   filelock_1.0.3   
[13] base64url_1.4     fastmap_1.2.0     lifecycle_1.0.4   memoise_2.0.1     compiler_4.4.1    codetools_0.2-20 
[19] Rcpp_1.0.13       rstudioapi_0.16.0 later_1.3.2       digest_0.6.37     R6_2.5.1          parallelly_1.38.0
[25] parallel_4.4.1    magrittr_2.0.3    bslib_0.8.0       tools_4.4.1       withr_3.0.1       txtq_0.2.4       
[31] mime_0.12         globals_0.16.3    cachem_1.1.0     

The example from the documentation

library(shiny)
library(ipc)
library(future)
library(promises)
plan(multicore)    # This will only work with multicore, which is unavailable on Windows

inaccessableAnalysisFunction <- function(){
  Sys.sleep(10)
  data.frame(result="Insightful analysis")
}

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Cancelable Async Task"),
  
  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      actionButton('run', 'Run'),
      actionButton('cancel', 'Cancel')
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      tableOutput("result")
    )
  )
)

server <- function(input, output) {
  
  fut <- NULL
  
  result_val <- reactiveVal()
  running <- reactiveVal(FALSE)
  observeEvent(input$run,{
    
    #Don't do anything if in the middle of a run
    if(running())
      return(NULL)
    running(TRUE)
    
    print("Starting Run")
    result_val(NULL)
    fut <<- future({
      result <- inaccessableAnalysisFunction()
    })
    prom <- fut %...>% result_val
    prom <- catch(fut,
                  function(e){
                    result_val(NULL)
                    print(e$message)
                    showNotification("Task Stopped")
                  })
    prom <- finally(prom, function(){
      print("Done")
      running(FALSE) #declare done with run
    })
    
    #Return something other than the future so we don't block the UI
    NULL
  })
  
  
  # Kill future
  observeEvent(input$cancel,{
    #
    # Use this method of stopping only if you don't have access to the
    # internals of the long running process. If you are able, it is
    # recommended to use AsyncInterruptor instead.
    #
    stopMulticoreFuture(fut)
  })
  
  
  output$result <- renderTable({
    req(result_val())
  })
}

# Run the application
shinyApp(ui = ui, server = server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions