Àngel R.
Àngel R.

Reputation: 106

Kind of arguments inheritance in nested functions in R?

I'm writing some functions in R and I'm having some issues. Summarizing, inside the function I'm writing, I call another function that I've developed. The 2nd function shares some arguments with the first, how to specify to this 2nd function that has to take the same values for its arguments that the ones in the first function?

first.fx=function(arg1,arg2,arg3,...){
  .
  .
  .
  second.fx=function(arg2,arg3,arg4,...){

  }
}

The second.fx shares with the first arg2 & arg3. How to inherit these to values to second.fx?

Upvotes: 0

Views: 387

Answers (2)

Hong Ooi
Hong Ooi

Reputation: 57686

You don't need to declare the arguments explicitly in the definition of second.fx. By the magic of lexical scoping, these variables will be found in second.fx's enclosing environment, which is that of first.fx.

first.fx <- function(arg1, arg2, arg3, ...)
{
     second.fx <- function(arg4)
     {
          # values of arg2/3 will be found from first.fx's environment
     }
}

Upvotes: 1

Stephan Kolassa
Stephan Kolassa

Reputation: 8267

Simply assign the values (which come from the call to first.fx as default parameters in the definition of second.fx:

second.fx <- function(arg2=arg2,arg3=arg3,arg4,...){

Upvotes: 3

Related Questions