Use Stdin from Within R Studio

How to input EOF in stdin in R?

The input is handled by the readline library common to other GNU project programs, and it commonly receives EOF when you press Ctrl-D.

Here is an example using littler (and I am not sure why every inout gets echoed back):

$ r -e 'print(summary(as.integer(readLines())))'
1
1
2
2
3
3
4
4
8
8 ## I pressed Ctrl-D here
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.0 2.0 3.0 3.6 4.0 8.0
$

My attempt to use a connection while trying to read in input causes R to freeze or crash

When working in RStudio, you need to use readLines(stdin()) rather than readLines(file('stdin')), though you can use either if running R in the terminal.

However, there is also an issue from not specifying the number of lines of input since you are using RStudio. When reading input from stdin, Ctrl+D signals the end of input. However, if you are doing this from RStudio rather than from the terminal Ctrl+D is unavailable, so without specifying the lines of input there is no way to terminate the reading from stdin.

So, if you are running R from the terminal, your code will work, and you signal the end of input via Ctrl+D. If you must work from RStudio, you can still use readLines(stdin()) if you know the number of lines of input; e.g.,

> readLines(stdin(), n=2)
Hello
World
[1] "Hello" "World"

An alternate workaround is to use scan(), e.g.:

> scan(,'')
1: Hello
2: World
3:
Read 2 items
[1] "Hello" "World"

(On the third line I just pressed Enter to terminate input). The advantage there is that you don't need to know the number of lines of input beforehand.

Have an Rscript read or take input from stdin

You need to read text from the connection created by file("stdin") in order to pass anything useful to the text argument of writeLines(). This should work

#!/usr/bin/Rscript 
writeLines(readLines(file("stdin")))

Rstudio's console stuck while interactive mode testing

Read from stdin(), not file("stdin"):

ans <- readLines(stdin(), n = 1)

From ?stdin:

stdin() refers to the 'console' and not to the C-level stdin of the process. The distinction matters in GUI consoles (which may not have an active stdin, and if they do it may not be connected to console input), and also in embedded applications. If you want access to the C-level file stream stdin, use file("stdin").

How to get a user input in command prompt and pass it to R

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!


Related Topics



Leave a reply



Submit