Is there a way to specify a *vector* of statements, as an argument to a function. 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>-- View this message in context: http://www.nabble.com/how-to-index-statements-provided-as-function-args--tp23056258p23056258.html Sent from the R help mailing list archive at Nabble.com.
Duncan Murdoch
2009-Apr-15 11:16 UTC
[R] how to index statements provided as function args?
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 >
That works perfectly. Thanks very much!! Duncan Murdoch-2 wrote:> > 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 >-- View this message in context: http://www.nabble.com/how-to-index-statements-provided-as-function-args--tp23056258p23057076.html Sent from the R help mailing list archive at Nabble.com.