Use stdin from studio R

When I try the following:

f<-file("stdin")
lines<-readLines(f)

from R-studio on Ubuntu I can enter text, but I cannot stop it. Ctr + C / D, a random keyboard hit won't help. He just hangs

I just found a sequel. How do I introduce EOF in stdin in R? but no help there - had to kill R-studio.

Does anyone have an explanation of what is wrong?

+3
source share
1 answer

Presumably, Rstudio redirects stdin, so it may not be properly accessible like "stdin"or "/dev/stdin". However, stdin()it still works.

I still could not type Ctrl + D. But you can read a fixed number of lines:

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

, . , 10 .

> a <- readLines(stdin(), n=10)
abc
def
ghi

# and now just keep pressing ENTER
...

> a <- a[a != ""]
> a
[1] "abc" "def" "ghi"

, Ctrl + D, .

:, stdin() Rscript: file("stdin"). , , readLines n=1 , . , , . read.table - Rstudio.

+4

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


All Articles