jeroen00ms
2011-Jul-23 13:31 UTC
[R] call a function with explicitly not setting an argument
Is there a way to call a function, and explicitly set an argument to 'not specified'? My situation is the following. I have a function which passes on most of its arguments to another function. The second function, myfun2, serializes all arguments and is out of my control. myfun <- function(...){ return(myfun2(...)); } now, the value for arguments of myfun are stored in variables. Say I want to call it with arguments 'foo' and 'bar', which are stored in variables 'myfoo' and 'mybar'. So in my script I call myfun(foo=myfoo, bar=mybar); However, I also want to be able to call myfun2 without any arguments. Is there any value that I can assign to myfoo and mybar, so that myfun(foo=myfoo, bar=mybar); is identical to myfun(); -- View this message in context: http://r.789695.n4.nabble.com/call-a-function-with-explicitly-not-setting-an-argument-tp3688883p3688883.html Sent from the R help mailing list archive at Nabble.com.
Joshua Wiley
2011-Jul-24 06:38 UTC
[R] call a function with explicitly not setting an argument
On Sat, Jul 23, 2011 at 6:31 AM, jeroen00ms <jeroen.ooms at stat.ucla.edu> wrote:> Is there a way to call a function, and explicitly set an argument to 'not > specified'? My situation is the following. I have a function which passes on > most of its arguments to another function. The second function, myfun2, > serializes all arguments and is out of my control. > > myfun <- function(...){ > ?return(myfun2(...)); > } > > now, the value for arguments of myfun are stored in variables. Say I want to > call it with arguments 'foo' and 'bar', which are stored in variables > 'myfoo' and 'mybar'. So in my script I call > > myfun(foo=myfoo, bar=mybar); > > However, I also want to be able to call myfun2 without any arguments. Is > there any value that I can assign to myfoo and mybar, so that > myfun(foo=myfoo, bar=mybar); is identical to myfun();I am not aware of any (which is not to say there is not), but what about catching ... between myfun and myfun2, and removing the arguments you do not want passed on? I had something like this in mind: myfun2 <- function(test, ...) { cat(missing(test), fill = TRUE) cat(..., fill = TRUE) } myfun <- function(...) { d <- list(...) index <- sapply(d, identical, y = "RemoveMe") d <- d[!index] do.call(myfun2, d) } myfun(foo = 1, test = "hi") myfun(foo = 1, test = "RemoveMe") Josh> > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/call-a-function-with-explicitly-not-setting-an-argument-tp3688883p3688883.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles https://joshuawiley.com/