Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

readLines function with new version of R

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.

like image 488
Dr.mincode Avatar asked May 16 '18 13:05

Dr.mincode


People also ask

What is use of readLines () in R?

readLines() function in R Language reads text lines from an input file. The readLines() function is perfect for text files since it reads the text line by line and creates character objects for each of the lines.

What package is readLines in R?

readLines [reader Package]

What is the purpose of the readLines () function?

readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.

How do I go from line to line in R?

By using R's pipe() command, and using shell commands to extract what we want, the full file is never loaded into R, and is read in line by line. It is this command that does all the work; it extracts one line from the desired file.


1 Answers

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.

like image 120
Ralf Stubner Avatar answered Sep 28 '22 06:09

Ralf Stubner