分配(...,envir = ...)和环境(...)=之间的区别
在环境中为名称赋值和设置变量的环境有什么区别?我无法从文档中弄清楚。
例如:
MyTestFunc = function(x)
{
myVal = "abcde"
# what is this doing? why is myVal not in the global environment after
# this call? I see it adds an environment attribute to the variable,
# but what good is that?
environment(myVal) = globalenv()
assign( "myVal" , myVal , envir = globalenv() )
# this seems to copy graphics:::rect to the current environment which seems
# to contradict the behavior of environment( myVal ) above
environment( rect ) = environment()
# this seems to do the same thing
assign( "rect" , rect , envir = globalenv() )
}
# this prints out rect, but shows <environment: namespace: graphics>!
rect
What is the difference between assigning a value to a name in an environment and setting the environment of a variable? I couldn't figure it out from the documentation.
for example:
MyTestFunc = function(x)
{
myVal = "abcde"
# what is this doing? why is myVal not in the global environment after
# this call? I see it adds an environment attribute to the variable,
# but what good is that?
environment(myVal) = globalenv()
assign( "myVal" , myVal , envir = globalenv() )
# this seems to copy graphics:::rect to the current environment which seems
# to contradict the behavior of environment( myVal ) above
environment( rect ) = environment()
# this seems to do the same thing
assign( "rect" , rect , envir = globalenv() )
}
# this prints out rect, but shows <environment: namespace: graphics>!
rect
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
分配函数只是将名称绑定到指定环境中的值。
但是环境替换函数做了两件事:它的主要目的是改变函数闭包的环境。该环境是函数体代码查找全局变量和函数的地方。通常,该环境是定义函数的环境(因此,如果您在提示符下定义它,它将使用 globalenv)。作为“奖励”,它只是为其他对象类型分配 .Environment 属性。这对于大多数对象来说毫无用处,但被公式使用。
第二件事是,它的工作方式几乎与任何其他替换函数类似:如果当前环境中存在该名称,它会直接修改它,否则它会创建一个本地副本并修改它。因此,在您的情况下,它会创建 rect 函数的本地副本并更改其环境。原有功能不变。
The assign function simply binds a name to a value in the specified environment.
But the environment replacement function does two things: its main purpose is to change the environment of a function closure. That environment is where the function's body code looks for global varables and functions. Normally, this envirionment is the one where the function was defined (so if you define it at the prompt it will use globalenv). As a "bonus", it just assigns the .Environment attribute for other object types. This is pretty useless for most objects, but is used by formulas.
The second thing is that it works like pretty much any other replacement function: if the name exists in the current environment it modifies it directly, otherwise it creates a local copy and modifies that. So in your case, it makes a local copy of the rect function and changes its environment. The original function remains unchanged.
在您询问并执行的函数内:
因此,您实际上并未对函数中名为“abcde”的
myVal
对象执行任何操作。您而是在函数环境中创建了一个名为“abcede”的新环境。然后执行:它创建了一个名为“myVal”的变量,其字符模式值为“abcde”,该值是从本地函数环境中收集的,并将其放入全局环境中。它现在有一个名为“.Environment”的属性。然而,您的目标是什么仍然不清楚,因为环境被设计用来定义功能的范围。为数据对象分配环境很奇怪。变量位于环境中,但设置变量的环境似乎没有什么用处。变量。所以我认为你的问题的答案:……那有什么好处?”应该是“这没有什么好处。”
Inside a function you asked and executed:
So you didn't really do anything to the
myVal
object named "abcde" that was in the function. You instead created a new environment named "abcede" inside the function's environment. You then executed:It created a variable named "myVal" with the character mode value of "abcde" gathered from the local function environment, and put it into the global environment. It does now have an attribute named ".Environment". However, it remains unclear what your goals are, since environments are designed to be used to define the scope of functions. Assigning an environment to a data object is just weird. Variables are in environments, but there wouldn't seem to be a useful purpose in setting the environment of a variable. So I think the answer to your question: ...what good is that?" should be "it's not any good."