I have code that is taking a long time to execute and I would like to know if there is a way to continue interacting with other elements of the application?
When you press the "go" button, a long calculation is run. I have another button "+", when it pressed increments a value which is displayed in a text field
app_server <- function(input, output, session) {
observeEvent(input$go, {
num_cores <- detectCores(); cl <- makeCluster(num_cores); registerDoParallel(cl)
l <- as.list(as.character(1:10)); groups <- as.integer(gl(length(l), 3, length(l))); chunk <- split(l, groups)
output$plot <- renderPlot({
withProgress(message = 'Calculation in progress',
detail = 'This may take a while...', value = 0, {
for (i in 1:length(chunk)) {
subl <- chunk[[i]]
res <- foreach(j = 1:length(subl), .combine = 'rbind',
.packages = c("dplyr", "caret")) %dopar% {
return(data.frame("index" = (i-1)*3 + j))}
print(res)
incProgress(1/length(l))
Sys.sleep(0.5)
}
env <- foreach:::.foreachGlobals; rm(list=ls(name=env), pos=env); stopCluster(cl)
})
plot(cars)
})
})
values <- reactiveValues(counter = 0);
observeEvent(input$inc, { values$counter <- values$counter + 1 })
output$value <- renderText({ values$counter })
}
app_ui <- function(request) { actionButton("go", "Go"), plotOutput("plot"), actionButton("inc", "+"), textOutput("value"))}