[Rd] readLines function with R >= 3.5.0

Ralf Stubner r@lf@@tubner @ending from d@q@n@@com
Mon May 28 12:13:57 CEST 2018


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>-D

From 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 using 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>


More information about the R-devel mailing list