From hb at stat.berkeley.edu  Tue Jan 29 07:19:15 2008
From: hb at stat.berkeley.edu (Henrik Bengtsson)
Date: Mon, 28 Jan 2008 22:19:15 -0800
Subject: [Rd] A "safe" do.call
In-Reply-To: <f8e6ff050801281819l223d85deu458c67fd6e0b3693@mail.gmail.com>
References: <f8e6ff050801281819l223d85deu458c67fd6e0b3693@mail.gmail.com>
Message-ID: <59d7961d0801282219x4954ebb3xfea03b7526debdf3@mail.gmail.com>

I tried to do this a couple of years ago, cf. doCall() in R.utils.
It's quite a tricky problem and I recall I wasn't perfectly happy with
the solution but it's a start.

/Henrik

On Jan 28, 2008 6:19 PM, hadley wickham <h.wickham at gmail.com> wrote:
> Has anyone developed a version of do.call that is safe in the sense
> that it silently drops parameters that do not appear in the formals of
> the called function? This is useful when ... ends up being used in
> multiple further functions.  e.g.
>
> f <- function(a, b) {a + b}
> do.call(f, list(a=1, b=2, c=3))  # Errors
> safe.call(f, list(a=1, b=2, c=3)) # Returns 3
>
> If have quickly thrown together the following, but it doesn't support
> position based calls, and I'd like to avoid reinventing the wheel.
>
> safe.call <- function(f, params, f.params = names(formals(f))) {
>  if ("..." %in% f.params) {
>    safe.params <- params
>  } else {
>    safe.params <- params[intersect(f.params, names(params))]
>  }
>  do.call(f, safe.params)
> }
>
> I hope to use safe.call to prevent a wide class of potential bugs in ggplot2.
>
> Hadley
>
> --
> http://had.co.nz/
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] A "safe" do.call
Henrik Bengtsson