[R] Pause script at input from terminal (interactive use)

Greg Snow 538280 @end|ng |rom gm@||@com
Thu Apr 18 20:47:24 CEST 2019

I am not an expert on Rscript, but I don't think that an actual
terminal is ever used when using Rscript.  And `interactive()` will
probably always be false.

So if you want the script to pause for input, you need to have some
form of user interface to work with.

One option is to use the tcltk package (this works on all OS's to my
knowledge, but not if you are accessing the computer remotely).  This
answer on stack overflow shows some code that may help:

On Thu, Apr 18, 2019 at 8:11 AM Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
> Dear all,
> I am trying to write an interactive script where the user type some
> input from the terminal. I used readline() but when I launch the file
> with Rscript, the function is overwritten directly, there is no
> waiting for the user's input. For instance, this example:
> VAR1 = as.numeric(readline(prompt = "Enter something -> "))
> VAR2 = as.numeric(readline(prompt = "Enter something else -> "))
> if(is.na(VAR1)) VAR1 = 0
> if(is.na(VAR2)) VAR2 = "empty"
> cat("Input was: ", VAR1, " - ", VAR2, "\n")
> is executed till the end without typing anything on terminal :
> $ Rscript test.R
> Enter something ->
> Enter something else ->
> Input was:  0  -  empty
> I also tried with ',1' at the end of readline, but the effect is the
> same. I should use the interactive() function but I am confused on its
> use.
> It is possible to launch R scritps in the interactive mode in the
> first place? and if yes, how? Or would python or julia be better
> choices in this case?
> Thank you.
> --
> Best regards,
> Luigi
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Gregory (Greg) L. Snow Ph.D.
538280 using gmail.com

More information about the R-help mailing list