R中嵌套函数中的参数继承类型?

时间:2015-01-08 09:39:46

标签: r

我在R中写了一些函数,我遇到了一些问题。总而言之,在我写作的功能中,我称之为我已经开发的另一个功能。第二个函数与第一个函数共享一些参数,如何指定第二个函数必须为第一个函数中的参数赋予相同的值?

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

  }
}

second.fx与第一个arg2& ARG3。如何将这些值继承到second.fx?

2 个答案:

答案 0 :(得分:2)

只需将值(来自first.fx的调用作为默认参数分配到second.fx的定义中:

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

答案 1 :(得分:1)

您不需要在second.fx的定义中明确声明参数。通过词法范围的魔力,这些变量可以在second.fx的封闭环境中找到,这是first.fx的环境。

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