[Rd] Is it possible to define a function's arguments via a wildcard in 'substitute()'?

Janko Thyson janko.thyson.rstuff at googlemail.com
Thu May 26 17:15:35 CEST 2011

Dear List,

just out of pure curiosity: is it possible to define a function via 
'substitute()' such that the function's formal arguments are specified 
by a "wildcard" that is substituted when the expression is evaluated?

Simple example:

x.args <- formals("data.frame")
x.body <- expression(
     out <- myArg + 100,

expr <- substitute(
     myFoo <- function(
         print("hello world!")

     list(ARGS=x.args, BODY=x.body)

# works

myFoo(a=1:3, stringsAsFactors=FALSE, myArg=5)
# does not work

It works for wildcard 'BODY' in the function's body, but not for 
wildcard 'ARGS' in the argument definition part of the function definition.

I thought that when writing a function that depends on some other 
function like 'data.frame()', it would maybe be possible not to 
'hardcode' the formal arguments of 'data.frame()' in the new function 
def but to have it mapped somewhat dynamically so that when 
'data.frame()' changes, the new function would change as well. This is 
probably a bad idea for countless reasons, nevertheless I'd be 
interested in learning if it's possible at all ;-)


More information about the R-devel mailing list