readLines function with new version of R

2019-02-16 13:14发布

问题:

My function is:

create_matrix <- function() {
  cat("Write the numbers of vertices: ")
  user_input <- readLines("stdin", n=1)
  user_input <- as.numeric(user_input)
  print(user_input)
  }

With the version 3.5.0, after i entered the data the program doesn't continue the execution. I'm calling the script directly from the console.

回答1:

My findings using various docker images:

  • The example works fine using R version 3.4.4 (2018-03-15) -- "Someone to Lean On" from rocker/r-ver:3.4.4.
  • The example hangs as described using R version 3.5.0 (2018-04-23) -- "Joy in Playing" from rocker/r-ver:3.5.0.
  • The example hangs as described using R Under development (unstable) (2018-05-19 r74746) -- "Unsuffered Consequences" from rocker/drd.

It looks as if the change mentioned in the release notes for version 3.5.1 is unrelated. I have sent my findings to r-devel and will report back the outcome:

  • The example works fine using R version 3.5.1 (2018-07-02) -- "Feather Spray"
  • The bug has been marked as fixed. I can assert that version R Under development (unstable) (2018-06-02 r74838) -- "Unsuffered Consequences" works as expected.

  • This is considered a bug, but it's unclear how and when it will be fixed.

  • A reasonable workaround: Send end-of-file (EOF, Ctrl-D) in addition to end-of-line.



回答2:

TLDR: Use stdin()

It works fine if you use stdin() instead of "stdin" .... which we would have recommended anyway.

But probably, for back compatibility "stdin" should probably work too ((or then signal a deprecation warning and work for now)