Stopping the script until the value is entered from the keyboard in R

I have a script, and I want to run it using keyboard input, and then continue working based on the value entered into this variable, I tried y=readline("please enter a value") , but the script will not wait for the value to be entered , it just displays this sentence and continues how can this be done ??? thanks in advance

+3
source share
3 answers

Here is a very simple #! a script that creates the foo() function, whose sole purpose is to drive away its argument to 'bar' .

 #! /home/gavin/R/2.13-patched/build/bin/Rscript --vanilla foo <- function(bar) { writeLines(paste("You supplied the following information:", bar)) } ## grab command arguments passed as -args args <- commandArgs(TRUE) ## run foo foo(args) 

We extract the command line arguments passed to the script from the shell using the commandArgs() function, and then pass them to foo() with the last line of the script.

If we have a bit of code in the foobar.R file, we can pass the argument and run the script using the Rscript interface. You also need to execute the above executable file ( chmod it).

Then the script can be called as it works as follows:

 [ gavin@desktop ~]$ ./foobar.R Cl You supplied the following information: Cl 

But pay attention to the information in ?Rscript , because, unfortunately, the standard Windows cmd shells are not aware of #! like scripts, so you might need to install another shell (help tells you that the Cygwin shell should work) in order to use the displayed functionality.

Update: Using source() and readline() .

An alternative, if you can do without having to work non-interactively (i.e. you do not open the R interface of R and do not run one line of code), this is just a source() script. For example, if it was in a call to script barfoo.R :

 dynamicwilcox <- function() { ANSWER <- readline("What column do you want to work on? ") if(ANSWER=="Ph") { writeLines("column was 'Ph'") } else if(ANSWER=="Cl") { writeLines("column was 'Cl'") } else { writeLines(paste("Sorry, we don't know what to do with column", ANSWER)) } ANSWER ## return something } dynamicwilcox() 

Then from the invitation of R Gui we could do:

 R> source("barfoo.R") What column do you want to work on? Cl column was 'Cl' 

or if you do not want to specify the full path, do the following:

 R> source(file.choose()) 

readline() works great when used in an interactive R session, and really is the best tool for the job - this is exactly what was done for it.

The whole premise that you want to run the script in batch mode, but provide some input, does not make much sense. R expects scripts to be autonomous when run in batch mode. You may not be aware of this, but when you double-click your script, it runs in batch mode.

+6
source

You probably want scan() , something like:

 print("please enter a value") y <- scan(file = "", what = "", nmax = 1) 

scan() will wait for user input, and any text will be saved in y - the mode character vector in this case.

+2
source

I had the same problem on Ubuntu (not Windows) and found a solution.

Use littler ( /usr/bin/r ) instead of Rscript and remember to pass the interactive -i flag to littler. This combination allows convincing readline() to work as desired in the script:

 #!/usr/bin/r -vi eprintf <- function(...) cat(sprintf(...), sep='', file=stderr()) prompt.read <- function(prompt="\n[hit enter to continue]: ") { eprintf("%s", prompt) invisible(readline()) } ans <- prompt.read('Please enter a value: ') eprintf("You have entered: '%s'\n", ans) # rest of script comes here... 

When I run it as a script, I get:

 $ ./rl.r Please enter a value: 42 You have entered: '42' $ ./rl.r Please enter a value: Hello to you! You have entered: 'Hello to you!' 

To install littler (on Ubuntu):

 sudo apt-get install littler 
0
source

Source: https://habr.com/ru/post/1347643/


All Articles