Skip to content

readLines function with R >= 3.5.0

6 messages · Martin Maechler, Michael Lawrence, Jennifer Lyon

#
Hi:

I have also just stumbled into this bug. Unfortunately, I can not
change the data my program receives from stdin. My code runs in a
larger system and stdin is sent to a Docker container running my R
code. The protocol is I read a line, readLines("stdin", n=1), do some
actions, send output on stdout, and wait for the next set of data.  I
don't have control over this protocol, so I can't use the ^D
workaround.

I am open for other workaround suggestions. The single line is
actually JSON and can be quite large. If there isn't something else
cleaner, I am going to try readChar() in a while loop looking for \n
but I'm guessing that would likely be too slow.  I am open to other
workaround solutions. For the moment I have reverted back to R 3.4.4.

Thanks for any suggestions.

Jen.
https://stackoverflow.com/questions/50372043/readlines-function-with-new-version-of-r
script
(unstable)
not

  
  
#
Hi Jen,

This was already resolved for R 3.5.1 by just disabling buffering on
terminal file connections like stdin.

Sounds like you might want to be running a web service or something
instead though.

Michael

On Tue, Jun 12, 2018 at 4:46 PM, Jennifer Lyon
<jennifer.s.lyon at gmail.com> wrote:
#
> Hi Jen, This was already resolved for R 3.5.1 by just
    > disabling buffering on terminal file connections like stdin.

and before R 3.5.1 exists, *and*
as the change is also not yet available in R patched (!)
this means using a version of
"R-devel", e.g. for Windows available from
   https://cloud.r-project.org/bin/windows/base/rdevel.html

Martin

    > Sounds like you might want to be running a web service or
    > something instead though.

    > Michael

    > On Tue, Jun 12, 2018 at 4:46 PM, Jennifer Lyon
> <jennifer.s.lyon at gmail.com> wrote:
>> Hi:
    >> 
    >> I have also just stumbled into this bug. Unfortunately, I
    >> can not change the data my program receives from
    >> stdin. My code runs in a larger system and stdin is sent
    >> to a Docker container running my R code. The protocol is
    >> I read a line, readLines("stdin", n=1), do some actions,
    >> send output on stdout, and wait for the next set of data.
    >> I don't have control over this protocol, so I can't use
    >> the ^D workaround.
    >> 
    >> I am open for other workaround suggestions. The single
    >> line is actually JSON and can be quite large. If there
    >> isn't something else cleaner, I am going to try
    >> readChar() in a while loop looking for \n but I'm
    >> guessing that would likely be too slow.  I am open to
    >> other workaround solutions. For the moment I have
    >> reverted back to R 3.4.4.
    >> 
    >> Thanks for any suggestions.
    >> 
    >> Jen.
    >> 
    >> 
    >>>> >>>>> 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
    >> 
    >> [[alternative HTML version deleted]]
    >> 
    >> ______________________________________________
    >> R-devel at r-project.org mailing list
    >> https://stat.ethz.ch/mailman/listinfo/r-devel

    > ______________________________________________
    > R-devel at r-project.org mailing list
    > https://stat.ethz.ch/mailman/listinfo/r-devel
#
Are you sure it's not available in patched? It's definitely in the
source since 6/1.

Michael


On Wed, Jun 13, 2018 at 2:19 AM, Martin Maechler
<maechler at stat.math.ethz.ch> wrote:
6 days later
#
Hi Michael:

I can confirm Martin's comment. I tested my software with r-devel (r74914)
and it works, while with r-patched (r74914) it does not work (it hangs, as
it did in R 3.5.0). I apologize for it taking so long for me to test this,
but is there any chance this fix could make into R 3.5.1?

Thanks.

Jen.

On Wed, Jun 13, 2018 at 6:24 AM, Michael Lawrence <lawrence.michael at gene.com

  
  
#
Hi Jen,

Please provide a reproducible example, since the original stack
overflow example works in both trunk and patched.

Thanks,
Michael

On Tue, Jun 19, 2018 at 3:45 PM, Jennifer Lyon
<jennifer.s.lyon at gmail.com> wrote: