readLines function with R >= 3.5.0

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

readLines function with R >= 3.5.0

Ralf Stubner
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 ..." 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.

All these tests where done using docker images from the rocker project,
i.e. effectively using Debian Linux.

Is this an error in the script or in R?

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: [hidden email]

Sitz: Potsdam
Register: AG Potsdam HRB 27966 P
Ust.-IdNr.: DE300072622
Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Martin Maechler
>>>>> 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


    > All these tests where done using docker images from the rocker project,
    > i.e. effectively using Debian Linux.

    > Is this an error in the script or in R?

    > 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: [hidden email]

    > Sitz: Potsdam
    > Register: AG Potsdam HRB 27966 P
    > Ust.-IdNr.: DE300072622
    > Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze

    > ______________________________________________
    > [hidden email] mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Martin Maechler
>>>>> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Gábor Csárdi
stdin() is not the same as file("stdin"), see the note in ?stdin.

Gabor

On Mon, 28 May 2018, 10:41 Martin Maechler, <[hidden email]>
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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Ralf Stubner
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: [hidden email]

Sitz: Potsdam
Register: AG Potsdam HRB 27966 P
Ust.-IdNr.: DE300072622
Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Martin Maechler
>>>>> Ralf Stubner
>>>>>     on Mon, 28 May 2018 12:13:57 +0200 writes:

    > On 28.05.2018 11:07, Gábor Csárdi wrote:
    >> stdin() is not the same as file("stdin"), see the note in ?stdin.

Of course it is not the same, otherwise using one for the other
had no effect, but you are right:
it is *NOT* a solution to the problem

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

oh dear...  yes, that is true ..


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

I'm glad to hear that.

Then, I now do think this needs to be dealt with as a bug
(but I'm not delving into fixing it!)

Martin


    > 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: [hidden email]

    > Sitz: Potsdam
    > Register: AG Potsdam HRB 27966 P
    > Ust.-IdNr.: DE300072622
    > Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze

    > x[DELETED ATTACHMENT signature.asc, application/pgp-signature]
    > ______________________________________________
    > [hidden email] mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Ralf Stubner
On 28.05.2018 16:38, Martin Maechler wrote:
> Then, I now do think this needs to be dealt with as a bug
> (but I'm not delving into fixing it!)

Ok. Can somebody with write privileges in bugzilla add the bug report? I
can also do this myself, if somebody with the required privileges can
create a user for me.

Greetings
Ralf

PS: I get an error message from https://bugs.r-project.org/bugzilla3/.

--
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: [hidden email]

Sitz: Potsdam
Register: AG Potsdam HRB 27966 P
Ust.-IdNr.: DE300072622
Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Martin Maechler
>>>>> Ralf Stubner
>>>>>     on Tue, 29 May 2018 11:21:28 +0200 writes:

    > On 28.05.2018 16:38, Martin Maechler wrote:
    >> Then, I now do think this needs to be dealt with as a bug
    >> (but I'm not delving into fixing it!)

    > Ok. Can somebody with write privileges in bugzilla add the
    > bug report? I can also do this myself, if somebody with
    > the required privileges can create a user for me.

    > Greetings Ralf

<< PS: I get an error message from   https://bugs.r-project.org/bugzilla3/.

Yes, it is currently "down", i.e., in a wrong state.
I had alerted the owner of the server a few hours ago, but as
that is in California, it may need another few hours
before one of the  R Core members can add an account for you on
R bugzilla.

Best, Martin Maechler

    > --
    > 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: [hidden email]

    > Sitz: Potsdam Register: AG Potsdam HRB 27966 P Ust.-IdNr.:
    > DE300072622 Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Simon Urbanek
The MySQL DB on the server died - not sure why. Restarted and it should be ok.


> On May 29, 2018, at 9:17 AM, Martin Maechler <[hidden email]> wrote:
>
>>>>>> Ralf Stubner
>>>>>>    on Tue, 29 May 2018 11:21:28 +0200 writes:
>
>> On 28.05.2018 16:38, Martin Maechler wrote:
>>> Then, I now do think this needs to be dealt with as a bug
>>> (but I'm not delving into fixing it!)
>
>> Ok. Can somebody with write privileges in bugzilla add the
>> bug report? I can also do this myself, if somebody with
>> the required privileges can create a user for me.
>
>> Greetings Ralf
>
> << PS: I get an error message from   https://urldefense.proofpoint.com/v2/url?u=https-3A__bugs.r-2Dproject.org_bugzilla3_&d=DwIDAw&c=LFYZ-o9_HUMeMTSQicvjIg&r=wCho7riGUuXpdLf26yPBz3JyaZUEU6lK6CD_m7-_CA8&m=_xu-PG0SXp13I6r_aA_W4Q0HuUKSUyN_7nwKZIVCqbs&s=L6IIRWOHJ6EkANE7wuzf8l4cEORIkFhwi-SE5i_iqII&e=.
>
> Yes, it is currently "down", i.e., in a wrong state.
> I had alerted the owner of the server a few hours ago, but as
> that is in California, it may need another few hours
> before one of the  R Core members can add an account for you on
> R bugzilla.
>
> Best, Martin Maechler
>
>> --
>> 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: [hidden email]
>
>> Sitz: Potsdam Register: AG Potsdam HRB 27966 P Ust.-IdNr.:
>> DE300072622 Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel