>>>>> Martin Maechler >>>>> on Mon, 28 May 2018 10:28:01 +0200 writes:>>>>> Ralf Stubner >>>>> on Fri, 25 May 2018 19:18:58 +0200 writes:>> Dear all, I would like to draw you attention to this >> question on SO: >> https://stackoverflow.com/questions/50372043/readlines-function-with-new-version-of-r >> Based on the OP's code I used the script >> ####################################### >> create_matrix <- function() { >> cat("Write the numbers of vertices: ") >> user_input <- readLines("stdin", n=1) >> user_input <- as.numeric(user_input) >> print(user_input) >> } >> create_matrix() >> ####################################### >> and called it with "R -f <filename>" from the command line. >> With 'R version 3.4.4 (2018-03-15) -- "Someone to Lean On"' the script >> prints the inputed number as expected. With both 'R version 3.5.0 >> (2018-04-23) -- "Joy in Playing"' and 'R Under development (unstable) >> (2018-05-19 r74746) -- "Unsuffered Consequences"' the script does not >> continue after inputing a number. > I can confirm. > It "works" if you additionally (the [Enter], i.e., EOL) you also > "send" an EOF -- in Unix alikes via <Ctrl>-D > The same happens if you use 'Rscript <filename>' > I'm not the expert here, but am close to sure that we (R core) > did not intend this change, when fixing other somewhat subtle > bugs in Rscript / 'R -f' > Martin Maechler The same behavior in regular R , no need for a script etc.> str(readLines("stdin", n=1))then in addition to the input you need to "give" an EOF (Ctrl D) in R >= 3.5.0 Interestingly, everything works fine if you use stdin() instead of "stdin" :> rr <- readLines(stdin(), n=1)foo> rr[1] "foo">---------- So, for now use stdin() which is much clearer than the string "stdin" anyway Martin Maechler
stdin() is not the same as file("stdin"), see the note in ?stdin. Gabor On Mon, 28 May 2018, 10:41 Martin Maechler, <maechler at stat.math.ethz.ch> wrote:> >>>>> Martin Maechler > >>>>> on Mon, 28 May 2018 10:28:01 +0200 writes: > > >>>>> Ralf Stubner > >>>>> on Fri, 25 May 2018 19:18:58 +0200 writes: > > >> Dear all, I would like to draw you attention to this > >> question on SO: > >> > https://stackoverflow.com/questions/50372043/readlines-function-with-new-version-of-r > > > >> Based on the OP's code I used the script > > >> ####################################### > >> create_matrix <- function() { > >> cat("Write the numbers of vertices: ") > >> user_input <- readLines("stdin", n=1) > >> user_input <- as.numeric(user_input) > >> print(user_input) > >> } > >> create_matrix() > >> ####################################### > > >> and called it with "R -f <filename>" from the command line. > > >> With 'R version 3.4.4 (2018-03-15) -- "Someone to Lean On"' the > script > >> prints the inputed number as expected. With both 'R version 3.5.0 > >> (2018-04-23) -- "Joy in Playing"' and 'R Under development > (unstable) > >> (2018-05-19 r74746) -- "Unsuffered Consequences"' the script does > not > >> continue after inputing a number. > > > I can confirm. > > It "works" if you additionally (the [Enter], i.e., EOL) you also > > "send" an EOF -- in Unix alikes via <Ctrl>-D > > > The same happens if you use 'Rscript <filename>' > > > I'm not the expert here, but am close to sure that we (R core) > > did not intend this change, when fixing other somewhat subtle > > bugs in Rscript / 'R -f' > > > Martin Maechler > > The same behavior in regular R , no need for a script etc. > > > str(readLines("stdin", n=1)) > > then in addition to the input you need to "give" an EOF (Ctrl D) in R >> 3.5.0 > > Interestingly, everything works fine if you use stdin() instead > of "stdin" : > > > rr <- readLines(stdin(), n=1) > foo > > rr > [1] "foo" > > > ---------- > > So, for now use stdin() which is much clearer than the string > "stdin" anyway > > Martin Maechler > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
On 28.05.2018 11:07, G?bor Cs?rdi wrote:> stdin() is not the same as file("stdin"), see the note in ?stdin.In particular stdin() works in an interactive session but not when R -f / Rscript is used, since it does not wait for the user to input anything: $ R -f readLines.R R version 3.5.0 (2018-04-23) -- "Joy in Playing" Copyright (C) 2018 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) [...]> create_matrix <- function() {+ cat("Write the numbers of vertices: ") + user_input <- readLines(stdin(), n=1) + user_input <- as.numeric(user_input) + print(user_input) + }> create_matrix()Write the numbers of vertices: numeric(0)>>>>>>>> Martin Maechler >>>>>>> on Mon, 28 May 2018 10:28:01 +0200 writes: >> > It "works" if you additionally (the [Enter], i.e., EOL) you also >> > "send" an EOF -- in Unix alikes via <Ctrl>-DFrom my point of view this is a reasonable workaround for now. Thanks Ralf -- Ralf Stubner Senior Software Engineer / Trainer daqana GmbH Dortustra?e 48 14467 Potsdam T: +49 331 23 61 93 11 F: +49 331 23 61 93 90 M: +49 162 20 91 196 Mail: ralf.stubner at daqana.com Sitz: Potsdam Register: AG Potsdam HRB 27966 P Ust.-IdNr.: DE300072622 Gesch?ftsf?hrer: Prof. Dr. Dr. Karl-Kuno Kunze -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: OpenPGP digital signature URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20180528/945902d8/attachment.sig>