When I try the following:
f<-file("stdin")
lines<-readLines(f)
from within R-studio on Ubuntu I can input text but unable to terminate it. Ctr+C/D, random hitting keyboard won't help. It simply hangs
I only found the followin so far How to input EOF in stdin in R? but no help there - had to kill R-studio.
Anybody have explanation what is wrong?
Presumably, Rstudio is redirecting stdin, so that it cannot be properly accessed as
"stdin"
or"/dev/stdin"
any longer. However,stdin()
still works.I was still unable to actually type Ctrl+D. But it is possible to read a fixed number of lines:
I've also discovered a hack that may help for interactive debugging. Let's say, you have at most 10 lines in your manual examples. Then you can do
If you run the same code in an environment where Ctrl+D is available, it also properly terminates the input.
Caveats: but
stdin()
does not work withRscript
: you'd have to switch back tofile("stdin")
. Furthermore, in some environments, if you usereadLines
withn=1
to read the file line-by-line, you may end up reopening the file and getting the first line every time. It seems that putting everything into a file and reading the whole file at once with e.g.read.table
is a much more robust way of developing with Rstudio.