[R] Detecting incomplete commands

Brian D Ripley ripley at stats.ox.ac.uk
Thu Dec 16 17:21:50 CET 2004


On 16 Dec 2004, Peter Dalgaard wrote:

> "Philippe Grosjean" <phgrosjean at sciviews.org> writes:
>
> > Thank you Thomas for your answer. This is of course what I intend to do
> > (indeed, to use a tryCatch(), instead of I try(). However, I would like to
> > have a different behaviour depending if the synthax is incorrect "ls())" for
> > instance, or incomplete "ls(" for instance. Indeed, exactly like the
> > distinction made at the command line:
> >
> > > ls()) # This generates an error
> > Error: syntax error
> > > ls( # This is not an error, but a multiline command
> > +
> >
> > If you use parse() within a try(), you got the same error message in both
> > cases: "Error in parse(file, n, text, prompt) : parse error". So, it is not
> > possible to discriminate between the two situation in this context. Is it a
> > way to cope with that?
>
> Here's one idea:
>
> > grep("line 2",try(parse(textConnection("ls)")),silent=T))
> numeric(0)
> > grep("line 2",try(parse(textConnection("ls(")),silent=T))
> [1] 1

This is a lot easier at C level: see <R_ext/Parse.h> and `Writing R
Extensions'.

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