Re: [Rd] clients and carriage returns

From: Prof Brian Ripley <>
Date: Wed, 20 Jun 2007 18:26:49 +0100 (BST)

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
> ______________________________________________
> mailing list

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Wed 20 Jun 2007 - 17:30:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 20 Jun 2007 - 23:35:12 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.