Dan Kelley
2008-Dec-19 00:27 UTC
[R] can a function alter the "..." argument and passed the result to another function?
Is there a way a function ('parent', say) can manipulate the ... argument, and then pass the manipulated value into another function ('child', say) that it calls? What I'm trying to do is to use some plotting defaults within 'parent' that are not part of parent's argument list, but to let the use over- ride these defaults if they choose. A sketch is below. parent <- function(stuff, ...) { dots <- list(...) if (!"type" in names(list)) dots$type='p' plot(stuff, unlist(dots)) # or alist()? or what? } I know, I can just make "type" be a formal argument of 'parent', and I'm happy to do that, but I'm still in the learning stages with R, and so I'm trying to become more familiar with as much R syntax as I can, even if I end up doing things differently later on. D. Kelley Oceanography Department, Dalhousie University Halifax, Nova Scotia, Canada
Uwe Ligges
2008-Dec-19 10:28 UTC
[R] can a function alter the "..." argument and passed the result to another function?
Dan Kelley wrote:> Is there a way a function ('parent', say) can manipulate the ... > argument, and then pass the manipulated value into another function > ('child', say) that it calls? > > What I'm trying to do is to use some plotting defaults within 'parent' > that are not part of parent's argument list, but to let the use > over-ride these defaults if they choose. A sketch is below. > > parent <- function(stuff, ...) { > dots <- list(...) > if (!"type" in names(list)) dots$type='p' > plot(stuff, unlist(dots)) # or alist()? or what? > }do.call() is your friend here, because you can easily pass a *list* of arguments to the function when calling it: parent <- function(stuff, ...) { dots <- list(...) if (!"type" %in% names(list)) dots$type <- 'l' do.call("plot", c(list(x=stuff), dots)) } Best, Uwe Ligges> I know, I can just make "type" be a formal argument of 'parent', and I'm > happy to do that, but I'm still in the learning stages with R, and so > I'm trying to become more familiar with as much R syntax as I can, even > if I end up doing things differently later on. > > D. Kelley > Oceanography Department, Dalhousie University > Halifax, Nova Scotia, Canada > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.