readLines function with new version of R
Asked Answered
D

2

11

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.

Deliciadelicious answered 16/5, 2018 at 13:17 Comment(16)
Can't you use data = readLines(n=1)?Polly
Yes, but it doesn't workDeliciadelicious
The Note in help("showConnections") might be relevant: " GUI consoles (which may not have an active ‘stdin’, and if they do it may not be connected to console input)".Polly
What does "doesn't work" mean specifically?Polly
Sorry, it continue the execution but it doesn't wait my input and it give me NA value.Deliciadelicious
Can you add some more details, in particular regarding how you "entered the data" with Rscript? If you can improve this question I'd consider attaching a bounty to it because I think it might be an interesting problem.Polly
First of all, i launch the script named graphi.R directly from terminal with an argument (for example graphi.R -c, to create a matrix(i'm using the argparse library for that)). The function create_matrix is called and ask me the number of vertices in my console. After i entered the number it's like it's waiting for another input (like a loop). With the older version i hadn't problem, i notice that they changed something in the readLines function. Thanks for your help.Deliciadelicious
Please edit this information (with the actual code and a minimal example of a script) into the question.Polly
I updated the question. So, i can confirm that the script works fine with the version 3.4.4_2.Deliciadelicious
The is a bug fix in R-patched concerning seekable streams and stdin so you may be hitting that.Weald
See also this: github.com/wch/r-source/blob/trunk/doc/NEWS.Rd#L113Polly
Thanks for your help, i understand the problem but i don't know how to solve it. So i'm back to the previous version.Deliciadelicious
@Deliciadelicious Can you post your Operating system type, version and rsession info? That should help a lot. i.e Are you on MacOSX, Windows or Unix OS. stdin has different properties across Windows and various flavors of Unix.Halftrack
I'm currently on arch.Deliciadelicious
Given the answer below, it is probably worth reporting this to R-devel.Judgment
@DirkEddelbuettel : No, that bug fix in R-patched does not fix the problem. But using stdin() {much more expressive anyway}, does work in all versions of R (see my A below).Reviviscence
G
6

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.

Garlaand answered 22/5, 2018 at 9:24 Comment(3)
can you share you docker script?Halftrack
@Halftrack All the docker images are available on docker hub via the rocker project. Anything else you are interested in?Garlaand
Thanks, based on the note I had presumed you had written a docker script that downloaded an image and then ran the code. If not no worries.Halftrack
P
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)

Provinciality answered 28/5, 2018 at 8:37 Comment(2)
stdin() works in an interactive session, but not when the script is started from the console. Your first suggestion on r-devel (send EOF / Ctrl-D) works fine.Garlaand
You are right, and I was wrong above: stdin() is not usable in a script, just interactively.Reviviscence

© 2022 - 2024 — McMap. All rights reserved.