Stop the time-consuming function by pressing the Shiny button

I apologize in advance if I ask a very simple question. I am new to R and Shiny, and I could not find a solution to this problem anywhere.

Here I have a very simple shinyApp that has two buttons in ui.R, the start button calls a function that works in a loop and says โ€œPrintโ€ something on the screen. I have a stop button that should be able to complete this loop.

I canโ€™t get the stop button to end the endless loop, Any ideas?

Below I have tried. Please forgive me if I do something wrong.

#ui.R library(shiny) shinyUI(fluidPage( actionButton("startSearch", label = "Start Search"), actionButton("stopSearch", label = "Stop Search") )) #server.R library(shiny) shinyServer(function(input, output, session){ initial.stop <- 0 observeEvent(input$startSearch, { while(TRUE) { Sys.sleep(1) if (initial.stop < input$stopSearch) { print('break') stop('error') } print("Infinite loop") } }) }) 

This is a problem because R is single-threaded and the application does not respond because it is stuck in an infinite loop. In fact, we have a lot of time for the calculation, which is performed in the for loop, we need to stop the calculation after the current iteration, when the user clicks the stop button.

Is there any way to approach this problem? Any suggestion would be helpful. Thanks

+5
source share

All Articles