Spurious output white line in R script (PR#8631)

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

Spurious output white line in R script (PR#8631)

pinard

I noticed that R scripts produce a spurious white line after output.
For example, the following shell script,


#!/bin/sh
R --slave --vanilla <<EOF

cat("Hello\n")

EOF


when made executable under the name ``hello`` along the search path, behaves
like this:


$ hello | od -bc
0000000 110 145 154 154 157 012 012
          H   e   l   l   o  \n  \n
0000007



The second newline is not wanted, and I would like if R was not
producing it.



--please do not edit the information below--

Version:
 platform = i686-pc-linux-gnu
 arch = i686
 os = linux-gnu
 system = i686, linux-gnu
 status =
 major = 2
 minor = 2.1
 year = 2005
 month = 12
 day = 20
 svn rev = 36812
 language = R

Locale:
LC_CTYPE=fr_CA.UTF-8;LC_NUMERIC=C;LC_TIME=fr_CA.UTF-8;LC_COLLATE=fr_CA.UTF-8;LC_MONETARY=fr_CA.UTF-8;LC_MESSAGES=fr_CA.UTF-8;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C

Search Path:
 .GlobalEnv, package:methods, package:stats, package:graphics, package:utils, package:datasets, fp.etc, package:grDevices, Autoloads, package:base

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

Re: Spurious output white line in R script (PR#8631)

Simon Urbanek
I don't see the bug here ... you may want to explain how this  
behavior conflicts with the documentation. As of your problem, see  
below.

On Feb 22, 2006, at 1:41 PM, [hidden email] wrote:

> I noticed that R scripts produce a spurious white line after output.
> For example, the following shell script,
>
>
> #!/bin/sh
> R --slave --vanilla <<EOF
>
> cat("Hello\n")
>
> EOF
>
>
> when made executable under the name ``hello`` along the search  
> path, behaves
> like this:
>
>
> $ hello | od -bc
> 0000000 110 145 154 154 157 012 012
>           H   e   l   l   o  \n  \n
> 0000007
>
>
>
> The second newline is not wanted, and I would like if R was not
> producing it.
>

If you don't want it, tell R to explicitly terminate in your script,  
e.g. with quit("no",0) - then there will be no new line.

Cheers,
Simon

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

Re: Spurious output white line in R script (PR#8631)

François Pinard
[Simon Urbanek]

>I don't see the bug here ... you may want to explain how this  behavior
>conflicts with the documentation.

Oh, sorry.  I merely surmised that R developers were aware of the
meaning of "--slave" option.  Within the output resulting of command
"man R", one reads:

       --slave
              Make R run as quietly as possible

So, I was not expecting R, running with that option activated, to
"volunteer" white lines. :-)

>If you don't want it, tell R to explicitly terminate in your script,
>e.g. with quit("no",0) - then there will be no new line.

Thanks for this work-around, which solves the problem of the spurious
white line at end of script.  I did not need arguments on the q() call,
first because the "no" is already implied through "default" from the
"--vanilla" option and second because 0 is also the default exit status.

Yet, it sounds reasonable to suggest (or hope) that R interprets an
end-of-file as meaning that R should cleanly quit.  I naively thought
that it does this already, from the fact that interactively at least,
R seems to behave on a Ctrl-D as if q() has been called.

This is either right or wrong using an end-of-file instead of calling
q().  If it is right to use end-of-file instead of an explicit q() call,
I'm reporting a bug.  If it is wrong to use end-of-file instead of an
explicit q() call, I'm rather making a suggestion. :-)

If you consider that the user is definitely wrong by not calling q()
explicitly, and that various unexpected things may then happen, R would
be friendlier if it produced a diagnostic, hitting an end-of-file while
expecting a statement.  Interactively, it could even go further, and
nicely remind the user, at the time the user types Ctrl-D, that calling
q() is the only proper way for cleanly terminating an R session.

>On Feb 22, 2006, at 1:41 PM, [hidden email] wrote:

>>I noticed that R scripts produce a spurious white line after output.
>>For example, the following shell script,


>>#!/bin/sh
>>R --slave --vanilla <<EOF

>>cat("Hello\n")

>>EOF


>>when made executable under the name ``hello`` along the search  
>>path, behaves
>>like this:


>>$ hello | od -bc
>>0000000 110 145 154 154 157 012 012
>>           H   e   l   l   o  \n  \n
>>0000007



>>The second newline is not wanted, and I would like if R was not
>>producing it.


>Cheers,
>Simon


--
François Pinard   http://pinard.progiciels-bpi.ca

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

Re: Spurious output white line in R script (PR#8631)

Brian Ripley
On Wed, 22 Feb 2006, François Pinard wrote:

> [Simon Urbanek]
>
>> I don't see the bug here ... you may want to explain how this  behavior
>> conflicts with the documentation.
>
> Oh, sorry.  I merely surmised that R developers were aware of the
> meaning of "--slave" option.

We are.

> Within the output resulting of command
> "man R", one reads:
>
>       --slave
>              Make R run as quietly as possible
>
> So, I was not expecting R, running with that option activated, to
> "volunteer" white lines. :-)

But Simon said `with the documentation'.  Not doing what you expected is
not a bug.  Can you please point us to documentation which says that
end-of-file produces no output?

This does appear to be deliberate behaviour from

void end_Rmainloop(void)
{
     Rprintf("\n");
     /* run the .Last function. If it gives an error, will drop back to
main
        loop. */
     R_CleanUp(SA_DEFAULT, 0, 1);
}

and I think it is necessary, as R might well have a partial line of output
queued up for the console.  So this is probably `as quietly as possible'.

[...]


--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Spurious output white line in R script (PR#8631)

Brian Ripley
On Thu, 23 Feb 2006, Thomas Lumley wrote:

> On Thu, 23 Feb 2006, Prof Brian Ripley wrote:
>
>> On Wed, 22 Feb 2006, François Pinard wrote:
> [...]
>>>
>>> So, I was not expecting R, running with that option activated, to
>>> "volunteer" white lines. :-)
>>
>> But Simon said `with the documentation'.  Not doing what you expected is
>> not a bug.  Can you please point us to documentation which says that
>> end-of-file produces no output?
>>
>> This does appear to be deliberate behaviour from
>>
>> void end_Rmainloop(void)
>> {
>>    Rprintf("\n");
>>    /* run the .Last function. If it gives an error, will drop back to main
>>       loop. */
>>    R_CleanUp(SA_DEFAULT, 0, 1);
>> }
>>
>> and I think it is necessary, as R might well have a partial line of output
>> queued up for the console.  So this is probably `as quietly as possible'.
>
> (While agreeing entirely on the "bug" issue), couldn't we have fflush()
> instead of sending a newline?
Well, we might not be outputting to a file ... (I did say console), and
.Last() might well produce output so we need to leave the console in a
suitable state.  I do think we need a \n unless we can be sure that we are
currently at the left margin of the console (and I am not sure we can be
unless we assume all output went through R[E]printf, and even in that
case we do not currently collect the information).

Consider the script

gannet% cat foo
#!/bin/sh
R --slave --vanilla <<EOF

.Last <- function() cat("goodbye\n")
cat("Hello")
EOF

which produces

gannet% foo.R
Hello
goodbye
gannet%

We do not want

gannet% foo.R
Hellogoodbye
gannet%

or at least, I do not want that.


Beyond that, there must be hundreds or more reference results of test runs
against which comparisons are made, and this might well affect a lot of
testing code, although perhaps we could conditionalize on --slave.

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Spurious output white line in R script (PR#8631)

Simon Urbanek
In reply to this post by Brian Ripley

On Feb 23, 2006, at 11:14 AM, Thomas Lumley wrote:

> On Thu, 23 Feb 2006, Prof Brian Ripley wrote:
>
>> On Wed, 22 Feb 2006, François Pinard wrote:
> [...]
>>> So, I was not expecting R, running with that option activated, to
>>> "volunteer" white lines. :-)
>>
>> But Simon said `with the documentation'.  Not doing what you  
>> expected is
>> not a bug.  Can you please point us to documentation which says that
>> end-of-file produces no output?
>>
>> This does appear to be deliberate behaviour from
>>
>> void end_Rmainloop(void)
>> {
>>    Rprintf("\n");
>>    /* run the .Last function. If it gives an error, will drop back  
>> to main
>>       loop. */
>>    R_CleanUp(SA_DEFAULT, 0, 1);
>> }
>>
>> and I think it is necessary, as R might well have a partial line  
>> of output queued up for the console.  So this is probably `as  
>> quietly as possible'.
>
> (While agreeing entirely on the "bug" issue), couldn't we have  
> fflush() instead of sending a newline?
>

It's not really about fflush - the rationale is to not leave non-
terminated lines on the output before quitting via EOF, which has its  
merits. If you remove it, you get this:
...
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

 > caladan:Rdev$

instead of

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

 >
caladan:Rdev$

fflush won't change this.

Cheers,
Simon

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

Re: Spurious output white line in R script (PR#8631)

Thomas Lumley
On Thu, 23 Feb 2006, Simon Urbanek wrote:

>
> On Feb 23, 2006, at 11:14 AM, Thomas Lumley wrote:
>
>> On Thu, 23 Feb 2006, Prof Brian Ripley wrote:
>>
>>> On Wed, 22 Feb 2006, François Pinard wrote:
>> [...]
>>>> So, I was not expecting R, running with that option activated, to
>>>> "volunteer" white lines. :-)
>>>
>>> But Simon said `with the documentation'.  Not doing what you expected is
>>> not a bug.  Can you please point us to documentation which says that
>>> end-of-file produces no output?
>>>
>>> This does appear to be deliberate behaviour from
>>>
>>> void end_Rmainloop(void)
>>> {
>>>   Rprintf("\n");
>>>   /* run the .Last function. If it gives an error, will drop back to main
>>>      loop. */
>>>   R_CleanUp(SA_DEFAULT, 0, 1);
>>> }
>>>
>>> and I think it is necessary, as R might well have a partial line of output
>>> queued up for the console.  So this is probably `as quietly as possible'.
>>
>> (While agreeing entirely on the "bug" issue), couldn't we have fflush()
>> instead of sending a newline?
>>
>
> It's not really about fflush - the rationale is to not leave non-terminated
> lines on the output before quitting via EOF, which has its merits.
I would have thought that, at least with --slave, you might want the
ability to produce output that didn't end with any newline (in contrast to
the original question, which was about one vs two newlines, which is easy
to fix).

  -thomas

Thomas Lumley Assoc. Professor, Biostatistics
[hidden email] University of Washington, Seattle
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Spurious output white line in R script (PR#8631)

François Pinard
In reply to this post by Brian Ripley
[Brian Ripley]
>[François Pinard]

>>Within the output resulting of command "man R", one reads:

>>       --slave
>>              Make R run as quietly as possible

>>So, I was not expecting R, running with that option activated, to
>>"volunteer" white lines. :-)

>Can you please point us to documentation which says that end-of-file
>produces no output?

I just did.  However, you state that "as quietly as possible" should not
be interpreted as "no output".  Our interpretations differ.

>This does appear to be deliberate behaviour from

>void end_Rmainloop(void)
>{
>     Rprintf("\n");
>     /* run the .Last function. If it gives an error, will drop back to
>main
>        loop. */
>     R_CleanUp(SA_DEFAULT, 0, 1);
>}

>and I think it is necessary, as R might well have a partial line of output
>queued up for the console.  So this is probably `as quietly as possible'.

I do not doubt that it is deliberate, and I suspected as much even
before submitting my initial report on this topic, as I do the same in
some interactive programs I wrote, so the shell prompt shows at the
left.  However, I'm careful at not doing it outside human-interactive
contexts.

It is likely unusual that R users starts an R session with "--slave",
when that session is really meant to be human-interactive.  Whatever the
documentation says or does not say, the spurious '\n' kludge has no good
reason to apply with "--slave".  Let's both be trying to have
a reasonable and intelligent conversation here, keeping in mind that the
documentation is not necessarily perfect either, and not the last word
of everything.

The argument that it is necessary to write a '\n' because a partial line
of output may be queued up for the console, does not fully hold.  Proof
is, following Simon Urbanek's suggestion, that the following valid
R script:


#!/bin/sh
R --slave --vanilla <<EOF

cat("Hello")
q()

EOF


produces such a partial line.  I see no problem there: when using
"--slave", a script writer should feel in good control of the produced
output, and s/he will recognise a missing newline as a bug in the
script, not as a bug in R.

If the newline is a way to flush out the output buffer before quitting,
the suggestion made by others to use `fflush()', or anything similar, is
wise.  Adding a newline when "--slave" has not been selected, and
whenever the output is connected to a tty, is also wise, regardless if
termination is effected through q() or through hitting end-of-file.

Not adding a newline in other circumstances is debatable, but wise.

It is also reasonable (whether documented or not) expecting that q() and
hitting end-of-file act similarly.  Some developers (or maybe none)
might even recognise they attempted it already.  Let me suggest that
this similarity is aimed, and even documented.

--
François Pinard   http://pinard.progiciels-bpi.ca

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

Re: Spurious output white line in R script (PR#8631)

François Pinard
[François Pinard, clarifying himself]

>Adding a newline when "--slave" has not been selected, and whenever the
>output is connected to a tty, is also wise, regardless if termination
>is effected through q() or through hitting end-of-file.

Just to make sure I'm not misinterpreted, the "and" of the first line
above should be read as a logical conjunction.

--
François Pinard   http://pinard.progiciels-bpi.ca

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