Roberto Brunelli
2007-May-31 13:20 UTC
[R] Selective 'expansion' of arguments in a match.call() result ...
Is it possible to write a support function to automatize selective argument expansion (based on argument value type) as in the following example, in order to write terse code even when there are many arguments? Forcing evaluation of all arguments is not a problem ... ______________________________________________Thanks a lot!__________R_ # When called with document = 1, we have the simple match.call() result, # when document = 2 and name is a string, it is expanded, otherwise it # is not example <- function (name, document = FALSE) { print(name) if(document == 1) { resh <- match.call() } else if (document == 2) { resh <- match.call() if(is.character(name)) { resh$name <- name } resh$document <- document } else { resh <- call("<undef>") } resh } > a <- "Roberto" > b <- 1 > example(a, document = 1) [1] "Roberto" example(name = a, document = 1) > example(a, document = 2) [1] "Roberto" example(name = "Roberto", document = 2) > example(b, document = 2) [1] 1 example(name = b, document = 2) > -- <r/> | Roberto Brunelli - [scientist at Fondazione Bruno Kessler-irst] | 'Home can be anywhere, for it is a part of one's self' ------------------ ITC -> dall'1 marzo 2007 Fondazione Bruno Kessler ITC -> since 1 March 2007 Fondazione Bruno Kessler
Gabor Grothendieck
2007-May-31 14:14 UTC
[R] Selective 'expansion' of arguments in a match.call() result ...
Try this: # ith arg is expanded if expand[[i]] is TRUE where expand is # extended to vector of same length as ... . # default is to expand all args if 1st arg is "character" example2 <- function(..., expand = is.character(..1)) { L <- list(...) expand <- rep(expand, length = length(L)) mc <- match.call() mc$expand <- NULL for(i in which(expand)) mc[[i+1]] <- L[[i]] mc } # test a <- b <- 1 example2(a, b) d <- "a" example2(d, b) example2(a, b, expand = TRUE) example2(a, b, expand = c(TRUE, FALSE)) On 5/31/07, Roberto Brunelli <brunelli at itc.it> wrote:> Is it possible to write a support function to automatize selective > argument expansion (based on argument value type) as in the following > example, in order to write terse code even when there are many > arguments? Forcing evaluation of all arguments is not a problem ... > > ______________________________________________Thanks a lot!__________R_ > > # When called with document = 1, we have the simple match.call() result, > # when document = 2 and name is a string, it is expanded, otherwise it > # is not > > example <- function (name, document = FALSE) { > > print(name) > > if(document == 1) { > resh <- match.call() > } else if (document == 2) { > resh <- match.call() > > if(is.character(name)) { > resh$name <- name > } > resh$document <- document > } else { > resh <- call("<undef>") > } > > resh > } > > > a <- "Roberto" > > b <- 1 > > example(a, document = 1) > [1] "Roberto" > example(name = a, document = 1) > > example(a, document = 2) > [1] "Roberto" > example(name = "Roberto", document = 2) > > example(b, document = 2) > [1] 1 > example(name = b, document = 2) > > > > -- > <r/> > | Roberto Brunelli - [scientist at Fondazione Bruno Kessler-irst] > | 'Home can be anywhere, for it is a part of one's self' > > ------------------ > ITC -> dall'1 marzo 2007 Fondazione Bruno Kessler > ITC -> since 1 March 2007 Fondazione Bruno Kessler > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >