[Rd] clients and carriage returns

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jun 20 19:26:49 CEST 2007

I think the question is how are you accessing the parser?  The only public 
interface is R_ParseVector, and that works like parse(text=):

> parse(text="abc\ndef")
expression(abc, def)
> parse(text="abc\r\ndef")
Error in parse(text = "abc\r\ndef") :
" syntax error, unexpected $undefined, expecting '\n' or ';' in "abc

This does not happen when reading from files, as the connection machinery 
converts line endings (or fgets does).  So I think the problem may be in 
how you (or PostgreSQL) are getting text from files.

The rules of R syntax are quite simple, e.g.

   Newlines have a function which is a combination of token separator and
   expression terminator.  If an expression can terminate at the end of
   the line the parser will assume it does so, otherwise the newline is
   treated as whitespace.  Semicolons (@samp{;}) may be used to separate
   elementary expressions on the same line.

(from R-lang.texi).

On Wed, 20 Jun 2007, Joe Conway wrote:

> I've recently ported PL/R (R embedded in Postgres as a procedural
> language) to work on Win32 -- thank you to all involved for making the
> embedded interface common between *nix and Win32!
> This of course means users are now creating PL/R functions using Win32
> based editors, which are using \r\n for EOL. I had not realized this
> before, but R throws a parse error when it sees \r, even on a Win32 OS.
> Near as I can tell browsing R source, this is true on all operating
> systems -- e.g. gram.y seems to look only for \n, and in
> src/gnuwin32/graphapp/events.c:handle_char(), \r is actively replaced
> with \n.

That's not part of R's I/O, though.

> I didn't find any specific mention of EOL/newline in "Writing R
> Extensions", and only reference to related escape sequences in "R
> Language Definition".
> My questions:
> - Is it safe (and indeed proper) for PL/R to always convert \r\n to
>   \n prior to invoking the R parser?
> - Should the same be done for \r to \n (e.g. on OS X)?
> - Any other considerations I should care for?
> Thanks,
> Joe
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

Brian D. Ripley,                  ripley at stats.ox.ac.uk
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

More information about the R-devel mailing list