You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
The text was updated successfully, but these errors were encountered:
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.
The text was updated successfully, but these errors were encountered: