From ligges at statistik.tu-dortmund.de  Sat Apr  5 21:35:50 2008
From: ligges at statistik.tu-dortmund.de (Uwe Ligges)
Date: Sat, 05 Apr 2008 21:35:50 +0200
Subject: [Rd] How to create a function calling two functions with
 unknown number of parameters?
In-Reply-To: <16501233.post@talk.nabble.com>
References: <16501233.post@talk.nabble.com>
Message-ID: <47F7D496.8080808@statistik.tu-dortmund.de>



ZT2008 wrote:
> 
> ... can be used to represent unknown number of parameters passed into a
> function. 
> 
> For example, I write a function g. g calls another function f1. 
> 
> For example f1 could be different random number generation function. 
> 
> when f1=rnorm(), it has 3 parameters n, mean and standard deviation.
> 
> when f1=rexp(), it has 2 parameters n and rate. 
> 
> g can be defined as
> 
> g <- function(f1, ...) {
> 	f1(...)
> }
> 
> My problem is what about g calls two functions with unknown number of
> parameters.
> 
> In this case one ... doesn't help. 
> 
> If I define g as follows:
> 
> g <- function(f1, f2, ...) {
> 	f1(...)+f2(...)
> }
> 
> It seems ... is only passed to f1, it can't be passed to f2. 


No, it is passed to both:

  g <- function(f1, f2, ...) {
      f1(...) + f2(...)
  }
  f1 <- function(a) print(a)
  f2 <- function(a) print(a)
  g(f1, f2, 5)

[1] 5
[1] 5
[1] 10



> Can anybody help me? Thanks!

Well, what you requested already works...

If you want to pass different arguments to f1 and f2, you might want to 
specify two *lists* of arguments for f1() and f2() in g() and call f1() 
and f2() by do.call() within g().

Uwe Ligges



[Rd] How to create a function calling two functions with unknown number of parameters?
Uwe Ligges