[R] how to index statements provided as function args?

Duncan Murdoch murdoch at stats.uwo.ca
Wed Apr 15 13:16:47 CEST 2009


On 15/04/2009 6:31 AM, Dan Kelley wrote:
> Is there a way to specify a *vector* of statements, as an argument to a
> function. 

There are several ways.  expression() converts its arguments into a 
vector of expressions, e.g.

 > e <- expression(cat("a\n"), cat("b\n"))
 > e[[1]]
cat("a\n")
 > eval(e[[1]])
a

Duncan Murdoch

  What I'm trying to do is to provide the ability to add elements
> to a series of panels drawn by a function.  (As documented, split.screen
> does not provide this capability.) My idea is to mimic the 'plot.axes'
> argument of filled.contour(), but with indexed statements to be executed.  A
> series of tests is given below.  (NOTE: I know that I can use parse() on
> character strings, but I'm trying to avoid forcing users of the function to
> deal with nested quote marks, and the filled.contour scheme seems elegant to
> my eye.)
> 
>> f<-function(x){x[[1]]}
>> f(list({cat("a\n")},{cat("b\n")}))
> a
> b
>> f(c({cat("a\n")},{cat("b\n")}))
> a
> b
> NULL
>> f({{cat("a\n")},{cat("b\n")}})
> Error: syntax error
>




More information about the R-help mailing list