From ripley at stats.ox.ac.uk  Wed Jun 20 19:26:49 2007
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed, 20 Jun 2007 18:26:49 +0100 (BST)
Subject: [Rd] clients and carriage returns
In-Reply-To: <46795C8C.8080304@joeconway.com>
References: <46795C8C.8080304@joeconway.com>
Message-ID: <Pine.LNX.4.64.0706201814350.28523@gannet.stats.ox.ac.uk>

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)
attr(,"srcfile")
<text>
> 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



[Rd] clients and carriage returns
Prof Brian Ripley