[R] Detecting incomplete commands

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Dec 16 16:20:59 CET 2004


"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


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list