Skip to content

R CMD BATCH on scripts without trailing newline

5 messages · Martin Maechler, Stephen Eglen, Jan T. Kim

#
If the last line of an R script does not have a trailing newline, a
small errror is produced at the end of the script.

Small example.  If file eg.r contains one line:
getwd()
and there is no newline after the closing paren

$ R CMD BATCH eg.r

produces an error:
$ cat eg.r.Rout 

R : Copyright 2005, The R Foundation for Statistical Computing
Version 2.1.1 Patched (2005-09-01), ISBN 3-900051-07-0

...
Error: syntax error
Execution halted
$ 

Is it worth changing the BATCH script so that it adds a newline before
adding the call to proc.time()?

Stephen
#
StEgl> If the last line of an R script does not have a
    StEgl> trailing newline, a small errror is produced at the
    StEgl> end of the script.

    StEgl> Small example.  If file eg.r contains one line:
    StEgl> getwd() and there is no newline after the closing
    StEgl> paren

    StEgl> $ R CMD BATCH eg.r

    StEgl> produces an error: $ cat eg.r.Rout

    StEgl> R : Copyright 2005, The R Foundation for Statistical
    StEgl> Computing Version 2.1.1 Patched (2005-09-01), ISBN
    StEgl> 3-900051-07-0

    StEgl> ...

    >> getwd()proc.time()
    StEgl> Error: syntax error Execution halted $

aahh, now I finally understand via some people append
those **ugly** unneeded ';' to the end of almost every line of R
code.  It would have helped here
:-) :-)

    StEgl> Is it worth changing the BATCH script so that it adds
    StEgl> a newline before adding the call to proc.time()?

Yes I think it would be.  This is trivial, at least for 
 <Rsrc>/src/scripts/BATCH
Slightly better but more tricky:  only append a newline "when needed".
Any idea for that?

You didn't tell us the *platform* you run R on
(and BATCH does depend on the platform),
but I know that it's a version of unix,  Linux I suppose?

BTW: The windows version of "R CMD BATCH" is actually
     *documented* do to work with files that don't end in newline.

Martin
#
> (and BATCH does depend on the platform),
 > but I know that it's a version of unix,  Linux I suppose?

Thanks Martin.  Yes, linux - scientific linux 3 here.
#
On Thu, Sep 01, 2005 at 01:39:52PM +0200, Martin Maechler wrote:

            
What is the problem with the semicolons? Ugliness is in the eye of
the beholder, evidently, but on a more objective level, terminating
statements by an explicit and visible separator seems like a very
reasonable idea to me.
I think this is a dangerous direction of thought. Having a "terminator
if apparently needed" semantics of newline has already brought us the
inconsistency of if / else parsing inside and outside of blocks. No more
of that, please.

Best regards, Jan
1 day later
#
StEgl> If the last line of an R script does not have a
    StEgl> trailing newline, a small errror is produced at the
    StEgl> end of the script.

    StEgl> Small example.  If file eg.r contains one line:
    StEgl> getwd() and there is no newline after the closing
    StEgl> paren

    StEgl> $ R CMD BATCH eg.r

    StEgl> produces an error: $ cat eg.r.Rout

    StEgl> R : Copyright 2005, The R Foundation for Statistical
    StEgl> Computing Version 2.1.1 Patched (2005-09-01), ISBN
    StEgl> 3-900051-07-0

    StEgl> ...

    >>> getwd()proc.time()
    StEgl> Error: syntax error Execution halted $

    MM> aahh, now I finally understand via some people append
    MM> those **ugly** unneeded ';' to the end of almost every
    MM> line of R code.  It would have helped here :-) :-)

    StEgl> Is it worth changing the BATCH script so that it adds
    StEgl> a newline before adding the call to proc.time()?

    MM> Yes I think it would be.  This is trivial, at least for
    MM> <Rsrc>/src/scripts/BATCH Slightly better but more
    MM> tricky: only append a newline "when needed".  Any idea
    MM> for that?

It's probably not worth the extra effort (I agree with Jan on
*that); I've added the obvious to the BATCH script used on
unix-alike platforms, now adding a newline before 'proc.time()'
unconditionally.  I hope people can live with an extra byte in
the output files.

Martin
MM> *documented* do to work with files that don't end in
    MM> newline.