[R] Selective 'expansion' of arguments in a match.call() result ...

Roberto Brunelli brunelli at itc.it
Thu May 31 15:20:11 CEST 2007


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



More information about the R-help mailing list