Thanks a lot Hadley, think I got it figured out now.

expr <- substitute(
     myFoo <- function(
         myArg=0
     ){
         print("hello world!")
         eval(BODY)
         if("..." %in% names(formals(as.character(sys.call())[1]))){
             print(data.frame(...))
         } else {
             print("extended arguments not available yet.")
         }
     },
     list(BODY=x.body)
)
eval(expr)
myFoo()
myFoo(myArg=100)
formals(myFoo)
formals(myFoo) <- c(formals(myFoo), formals(data.frame))
formals(myFoo)
myFoo()
myFoo(myArg=100)
myFoo(a=1:5, b=1:5, myArg=100)
eval(call("myFoo", a=1:5, b=1:5))
do.call("myFoo", args=list(a=1:5, b=1:5))

One more question: is it possible to "augment" a function's body as I 
did with the function's arguments?
If there'd be a method that coerces objects of class "list" to class "{" 
and 'body()' would allow assignments of that class, you could run 
something like:

body(myFoo) <- as.curlyBracket(append(as.list(body(myFoo)), 
list(expression(eval(print("blabla"))))))

But I guess there isn't. Enough with playing around ;-)

Thanks,
Janko


On 26.05.2011 18:08, Hadley Wickham wrote:
> I think for the case where you want to built up a call from a function
> name + list of arguments, it's best to use call or as.call:
>
> call("f", a = 1, b = 2, c = 3)
>
> or if you already have the list:
> l<- list(as.name("f"), a = 1, b = 2, c = 3)
> as.call(l)
>
> Hadley
>
> On Thu, May 26, 2011 at 10:15 AM, Janko Thyson
> <janko.thyson.rst...@googlemail.com>  wrote:
>> 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,
>>     return(out)
>> )
>>
>> expr<- substitute(
>>     myFoo<- function(
>>         ARGS,
>>         myArg
>>     ){
>>         print("hello world!")
>>         print(ARGS)
>>         eval(BODY)
>>
>>     },
>>     list(ARGS=x.args, BODY=x.body)
>> )
>>
>> eval(expr)
>> myFoo(myArg=5)
>> # 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 ;-)
>>
>> TIA,
>> Janko
>>
>> ______________________________________________
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
>


-- 
------------------------------------------------------------------------

*Janko Thyson*
janko.thy...@googlemail.com <mailto:janko.thy...@googlemail.com>

Jesuitenstraße 3
D-85049 Ingolstadt

Mobile: +49 (0)176 83294257

This e-mail and any attachment is for authorized use by the intended
recipient(s) only. It may contain proprietary material, confidential
information and/or be subject to legal privilege. It should not be
copied, disclosed to, retained or used by any other party.
If you are not an intended recipient then please promptly delete this
e-mail and any attachment and all copies and inform the sender.


        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to