r - How to bind function arguments -


how partially bind/apply arguments function in r?

this how far got, realized approach doesn't work...

bind <- function(fun,...) {   argnames <- names(formals(fun))   bindedargs <- list(...)   bindednames <- names(bindedargs)   function(argnames[!argnames %in% bindedargs])    {    #todo   } } 

thanks!

have tried looking @ roxygen's curry function?

> library(roxygen)  > curry function (fun, ...)  {     .orig = list(...)     function(...) do.call(fun, c(.orig, list(...))) } <environment: namespace:roxygen> 

example usage:

> aplusb <- function(a,b) { +   + 2*b + } > oneplusb <- curry(aplusb,1) > oneplusb(2) [1] 5 

edit: curry concisely defined accept named or unnamed arguments, partial application of fun arguments way of formal() assignment requires more sophisticated matching emulate same functionality. instance:

> bind <- function(fun,...) + { +   argnames <- names(formals(fun)) +   boundargs <- list(...) +   boundnames <- names(boundargs) +   if(is.null(boundnames)) { +     formals(fun)[1:length(boundargs)] <- boundargs +   } else { +     formals(fun)[match(names(boundargs),argnames)] <- boundargs +   } +   fun + } > oneplusb <- bind(aplusb,1) > oneplusb(2) error in 2 * b : 'b' missing 

because first argument in function still a, need specify argument 2 intended (b=), or pass second argument.

> oneplusb function (a = 1, b)  {     + 2 * b } > oneplusb(b=2) ## or oneplusb(,2) [1] 5 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -