[R] Executing a code until a new user input aborts it (readlines?)

Hadassa Brunschwig
Sun, 15 Sep 2013 09:42:57 -0000

Hi,

I know that with readlines you can start executing a code upon user input.
What I would like to do is to have the ability to abort a code based on a
new user input. E.g. when the user hits enter the code stops running. That
is, I would periodically check for a user input and stop the code when the
correct input was entered.

Below is an example of a function which would run an endless loop but check
in each iteration if there was a new user input which would abort it. It
does not do the right thing obviously but maybe someone can point out how
this can be done.

startSystem <- function(){

    cat("Processing code, hit ENTER to stop.")
    i=a=n=1
    while(!is.na(n)){
      a=i+a
      i=i+1
      n = readline("")
    }
    return(NULL)

}

Thanks!

	[[alternative HTML version deleted]]