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.

Roland
  • 117,893
  • 9
  • 163
  • 255
Dr.mincode
  • 121
  • 5
  • Can't you use `data = readLines(n=1)`? – Roland May 16 '18 at 13:22
  • Yes, but it doesn't work – Dr.mincode May 16 '18 at 13:28
  • 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)". – Roland May 16 '18 at 13:28
  • 1
    What does "doesn't work" mean specifically? – Roland May 16 '18 at 13:30
  • Sorry, it continue the execution but it doesn't wait my input and it give me NA value. – Dr.mincode May 16 '18 at 13:33
  • 1
    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. – Roland May 17 '18 at 06:40
  • 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. – Dr.mincode May 17 '18 at 13:53
  • 1
    Please edit this information (with the actual code and a minimal example of a script) into the question. – Roland May 17 '18 at 13:57
  • I updated the question. So, i can confirm that the script works fine with the version 3.4.4_2. – Dr.mincode May 17 '18 at 14:59
  • 3
    The is a bug fix in R-patched concerning seekable streams and `stdin` so you may be hitting that. – Dirk Eddelbuettel May 17 '18 at 15:00
  • See also this: https://github.com/wch/r-source/blob/trunk/doc/NEWS.Rd#L113 – Roland May 18 '18 at 06:38
  • 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. – Dr.mincode May 21 '18 at 10:47
  • @Dr.mincode 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. – Technophobe01 May 23 '18 at 12:41
  • I'm currently on arch. – Dr.mincode May 23 '18 at 18:01
  • Given the answer below, it is probably worth reporting this to R-devel. –  May 25 '18 at 10:49
  • @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). – Martin Mächler May 28 '18 at 08:40

2 Answers2

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.

Ralf Stubner
  • 24,387
  • 3
  • 31
  • 63
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)

Martin Mächler
  • 4,107
  • 22
  • 24
  • 1
    `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. – Ralf Stubner May 28 '18 at 10:19
  • You are right, and I was wrong above: `stdin()` is not usable in a script, just interactively. – Martin Mächler May 29 '18 at 21:43