Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

future_promise behaves differently on shinyapps.io #108

Open
karchjd opened this issue Oct 14, 2024 · 0 comments
Open

future_promise behaves differently on shinyapps.io #108

karchjd opened this issue Oct 14, 2024 · 0 comments

Comments

@karchjd
Copy link

karchjd commented Oct 14, 2024

The example app below works for me locally but not on shinyapps.io. Specifically, if you press "Cancel," the task stops locally but not on shinyapps.io. Interestingly, it used to work, even with the same version of the package (in a larger app, see rstudio/rsconnect#1105).

I know this is not the best way to handle aborting, but within the larger app and given the constraints I’m working with, it seems to be the only feasible option.

library(shiny)
future::plan(future::multisession)



# 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")
    )
  )
)

long_run_fun <- function(){
  start_time <- Sys.time()
  while (as.numeric(Sys.time() - start_time) < 10) {
    Sys.sleep(0.1)
    if (file.exists(abort_file)) {
      quit()
    }
  }
} 

server <- function(input, output) {
  fut <- NULL

  abort_file_global <- reactiveVal()
  running <- reactiveVal(FALSE)
  result_val <- reactiveVal()

  abort_file <- tempfile()
  abort_file_global(abort_file)
  showNotification("Starting Run")

  observeEvent(input$run, {
    # Don't do anything if in the middle of a run
    if (running()) {
      showNotification("Already Running")
      return(NULL)
    }
    running(TRUE)

    print("Starting Run")
    fut <- promises::future_promise({
      long_run_fun()
    }, globals = c("abort_file", "long_run_fun"))
    promises::then(
      fut,
      function(value) {
        print("Task Completed")
        showNotification("Task Completed")
      }
    )

    promises::catch(
      fut,
      function(e) {
        print("Task Aborted")
        showNotification("Task Aborted")
      }
    )

    promises::finally(fut, function() {
      running(FALSE)
      if (file.exists(abort_file)) {
        file.remove(abort_file)
      }
    })

    # Return something other than the future so we don't block the UI
    return(NULL)
  })


  # Kill future
  observeEvent(input$cancel, {
    abort_file_global()
    file.create(abort_file_global())
  })


  output$result <- renderTable({
    req(result_val())
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant