4

I have a R function :

smallFunction <- function(x,y=2,z=3){ x+y+z }

I want to define the result of this function as the default value of a parameter of a bigger one :

bigFunction <- function(a,b,x,c=smallFunction(x,y,z))

How can I define y and z values ? Knowing that they could be define or left at their default values. I don't really want to do such a thing :

bigFunction <- function(a,b,x,c=smallFunction(x,y,z),y=2,z=3)

because in reality I have a lot of default parameters for the smallFunction

I would rather something like :

bigFunction <- function(a,b,x,c=smallFunction(x,y,z),...)

Thanks

  • What does it mean that y and z could be defined? They could exist in the global environment? – Frank Aug 04 '15 at 22:00
  • 1
    THe user should be able to enter manually the value when calling the function without storing the values in global environement. Is their any way to do that ? – Alexandre Chevaucher Aug 04 '15 at 22:08
  • 1
    Okay, that makes sense. Yes, the answer below and my comment on it are alternative ways to do it. – Frank Aug 04 '15 at 22:09

1 Answers1

3

You can pass ... from bigFunction to smallFunction

smallFunction <- function(x, y=2, z=3){ x+y+z }
bigFunction <- function(a,b,x, func=smallFunction, ...) { a+b+x+func(x=x, ...) }

For example,

bigFunction(1, 2, 3, y=10)
# [1] 22

As mentioned by @Frank you could do something like this to simplify the function body

bigFunction <- function(a,b,x, func=smallFunction, ...) {
    tmp <- func(x=x, ...)
    a+b+x+tmp
}
Rorschach
  • 31,301
  • 5
  • 78
  • 129