【发布时间】:2019-05-09 09:15:11
【问题描述】:
如果您想在函数g 中声明一个新变量,我知道您可以使用<<- 将其声明为全局变量。
g=function(t){
a<<-t
}
g(0)
print(a)
#It gives "0"
如果函数 g 已经在另一个函数 f 中,并且您希望函数 g 在函数 f 中声明一个新变量但不是全局变量,该怎么办?
g=function(t){
#declare the variable a and assign it the value t
}
f=function(t){
g(t)
return(a)
}
f(0)
#It should give 0.
print(a)
#It should say that the variable a is unknown.
【问题讨论】:
-
来自 g 函数,它声明了一个
-
a <- g(t);return(a)是一个选项吗?