考虑这个函数plus_x
:
y <- 1
plus_x <- function(input, x = y){
return(input + x)
}
在这里,x
的y
默认值在函数调用期间进行计算.
y <- 1
plus_x <- function(input, x = y){
return(input + x)
}
y <-10
plus_x(1)
# > 11
有没有办法将y
的值"固定"到函数定义期间的状态?
目标:
y <- 1
plus_x <- function(input, x = y){
# y is now always 1
return(input + x)
}
y <-10
plus_x(1)
# > 2