call a function with explicitly not setting an argument

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

call a function with explicitly not setting an argument

Jeroen Ooms.
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();



Reply | Threaded
Open this post in threaded view
|

Re: call a function with explicitly not setting an argument

Joshua Wiley-2
On Sat, Jul 23, 2011 at 6:31 AM, jeroen00ms <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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/

______________________________________________
[hidden email] 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.