简体   繁体   中英

R function '…' argument scope

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM