R函数'...'的参数范围 [英] R function '...' argument scope

查看:102
本文介绍了R函数'...'的参数范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  f1 < -  function(x,...){
print(y)
}

f1(x = 1,y = 2)

或此代码通过source()

  f1 < -  function(x,...) {
y< - 2
f2(x,y = y,...)
}

f2 < - 函数(x,...) {
print(y)
}

f1(x = 1)

遇到这个错误

  print(y)中的错误:找不到对象'y'

我估计'...'参数来自全球环境吗?

解决方案

你应该在你的函数中调用y,像这样是正确的

  f1 < -  function(x,...){
l < - list(...)
if(!is.null(l $ y))print(l $ y)
}
f1(x = 1,y = 2)


Tried this code via source()

f1 <- function(x, ...){
  print(y)
}

f1(x = 1, y = 2)

or this code via source()

f1 <- function(x, ...){
  y <- 2
  f2(x, y = y, ...)
}

f2 <- function(x, ...){
  print(y)
}

f1(x = 1)

Got this Error

Error in print(y) : object 'y' not found

I guess the '...' argument takes from the global environment?

解决方案

you should call y in your function as correct like this

f1 <- function(x, ...){
l <- list(...)
if(!is.null(l$y)) print(l$y)
}
f1(x = 1, y=2)

这篇关于R函数'...'的参数范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆