[Rd] [R] Continuation and parse

Martin Morgan mtmorgan at fhcrc.org
Fri Jul 14 06:47:48 CEST 2006

[this is from R-help, at the end of June]

Jim Lemon asked about parsing syntactically incorrect versus
incomplete lines, generating a couple of responses.

Philippe's suggestion isn't robust (e.g., "\nls)". Prof. Ripley's
comment lead me to R_ParseVector (the only exposed parse
routine). Unfortunately, this returns from parsing incomplete or
syntactically incorrect text with a PROTECT stack imbalance, so there
is no natural way to return from a .Call.

What I'd like is to have access to multiline console input after
parsing but before evaluation, or to return from a .Call like that
below with either the parsed expression or information about the
reason for failure. Any suggestions?

Thanks in advance,


SEXP parses(SEXP cmd) {
  ParseStatus status;
  SEXP res;
  res = R_ParseVector(cmd, 1, &status);
  return res;

/* > .Call("parses", "ls(") */
/* 25        SEXP res = R_ParseVector(cmd, 1, &status); */
/* (gdb) p R_PPStackTop */
/* $1 = 3 */
/* (gdb) next */
/* 26        return res; */
/* (gdb) p R_PPStackTop */
/* $2 = 5 */

R version 2.4.0 Under development (unstable) (2006-07-11 r38560)

Philippe Grosjean <phgrosjean at sciviews.org> writes:

> Well, you haven't used the search engines with the right key: the magic 
> words are:
>  > RSiteSearch("incomplete line")
> With the first document being my query (almost two years ago), and the 
> second one being Peter Dalgaard answer. You must adapt it to cope with 
> internationalization, but basically, you could use something like:
>  > grep("\n2:",try(parse(textConnection("ls)")), silent = TRUE))
> numeric(0)
>  > grep("\n2:",try(parse(textConnection("ls(")), silent = TRUE))
> [1] 1

From: Prof Brian Ripley <ripley>
Date: Wed, 28 Jun 2006 13:44:49 +0100 (BST)

On Wed, 28 Jun 2006, Jim Lemon wrote:

> Hi gurus,
> After an unsuccessful scrabble through the documentation and Jon's
> excellent search facility, I am no wiser as to how R recognizes an
> incomplete command line and politely raises its hand for more. The help
> page for parse gives no indication that it does anything more than spit
> the dummy when fed an incomplete command line, but something in there
> must recognize such ellipsis. Any hints?

It's internal. Look in src/main/main.c, in particular the R_Repl*
functions. In short, R_Parse1Buffer can return PARSE_INCOMPLETE.

More information about the R-devel mailing list