My attempt to use a “connection” when trying to read in input causes R to freeze or crash

Sorry, but the terminology that I use in the title may not be used correctly. Whenever I try to run this code, it seems to try to run it, but never completes the command. When I press the stop sign (red), it does nothing. I can’t close out of R. So why is this happening forever to run?

con <- file('stdin', open = 'r')

inputs <- readLines(con)
+4
source share
2 answers

When working in RStudio you need to use readLines(stdin()), not readLines(file('stdin')), although you can use either when you start R in the terminal.

, , RStudio. stdin, Ctrl + D . , RStudio, Ctrl + D , stdin.

, R , , Ctrl + D. RStudio, readLines(stdin()) ; .

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

scan(), :

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

( Enter ). , .

+5

RStudio R ( 4 stdin ). , . , , stdin() readLines ( , ). @duckmayr scan() , ...

. "", ( -, stdin() R ).

"okay", , .. , ( "EOF" ).

input <- function() {
  entry <- ''
  while (!any(entry == "EOF")) {
    entry <- c(readline(), entry)  
  }
  return(entry[-1])
}
+1

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


All Articles