R function '…' argument scope

橙三吉。 提交于 2019-12-13 21:49:23

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/45249724/r-function-argument-scope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!