4

I am trying to use

var <- as.numeric(readline(prompt="Enter a number: "))

and later use this in a calculation.

It works fine when running in RStudio but I need to be able to pass this input from the command line in Windows 10 I am using a batch file with a single line

Rscript.exe "C:\My Files\R_scripts\my_script.R"

When it gets to the user input part it freezes and it doesn't provide expected output.

Squashman
  • 13,649
  • 5
  • 27
  • 36
Sir Oliver
  • 57
  • 8
  • Does this answer your question? [How can I read command line parameters from an R script?](https://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script) – Peace Wang Dec 29 '21 at 15:43
  • `readline` only waits for user input if run on its own. You are running a full script so it will run until the end of the script and assign `NA` to `var`. – Rui Barradas Dec 29 '21 at 15:44
  • I imagine because executing via Rscript doesn't give you an interactive environment - as automated scripts will never proceed. See the the help on `?readline` example. –  Dec 29 '21 at 15:46

1 Answers1

8

From the documentation of readline():

This can only be used in an interactive session. [...] In non-interactive use the result is as if the response was RETURN and the value is "".

For non-interactive use - when calling R from the command line - I think you've got two options:

  1. Use readLines(con = "stdin", n = 1) to read user input from the terminal.
  2. Use commandArgs(trailingOnly = TRUE) to supply the input as an argument from the command line when calling the script instead.

Under is more information.

1. Using readLines()

readLines() looks very similar to readline() which you're using, but is meant to read files line by line. If we instead of a file points it to the standard input (con = "stdin") it will read user input from the terminal. We set n = 1 so that it stops reading from the command line when you press Enter (that is, it only read one line).

Example

Use readLines() in a R-script:

# some-r-file.R

# This is our prompt, since readLines doesn't provide one
cat("Please write something: ")
args <- readLines(con = "stdin", n = 1)

writeLines(args[[1]], "output.txt")

Call the script:

Rscript.exe "some-r-file.R"

It will now ask you for your input. Here is a screen capture from PowerShell, where I supplied "Any text!".

Print screen from PowerShell

Then the output.txt will contain:

Any text!

2. UsingcommandArgs()

When calling an Rscript.exe from the terminal, you can add extra arguments. With commandArgs() you can capture these arguments and use them in your code.

Example:

Use commandArgs() in a R-script:

# some-r-file.R
args <- commandArgs(trailingOnly = TRUE)

writeLines(args[[1]], "output.txt")

Call the script:

Rscript.exe "some-r-file.R" "Any text!"

Then the output.txt will contain:

Any text!
jpiversen
  • 3,062
  • 1
  • 8
  • 12
  • ok. but how can I make it so that the user gets a prompt and then enters a value which will later be used? – Sir Oliver Dec 30 '21 at 07:55
  • I did some more research, and have updated my answer with a solution for that. I think this should solve your problem. Please let me know how it went. – jpiversen Dec 30 '21 at 15:41