6

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

Here, I have a very basic shinyApp that has two buttons in ui.R, Start button will call a function that runs in a loop and say Prints something on the screen. I have a stop button which should be able to terminate this loop.

I am not able to get the stop button to terminate the infinite loop, Any ideas?

Following is that i have tried. Please pardon me if I am doing 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")
     }
   })
})

Is this an issue since R is single threaded and Application is non responsive as its stuck in an infinite loop. In reality we have a big time consuming computation that runs in a for loop, we need to stop the computation after the current iteration when the user clicks stop.

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

joemath
  • 83
  • 1
  • 4

0 Answers0