分配(...,envir = ...)和环境(...)=之间的区别

发布于 2024-12-22 19:39:51 字数 755 浏览 2 评论 0原文

在环境中为名称赋值和设置变量的环境有什么区别?我无法从文档中弄清楚。

例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神魇的王 2024-12-29 19:39:51

分配函数只是将名称绑定到指定环境中的值。

但是环境替换函数做了两件事:它的主要目的是改变函数闭包的环境。该环境是函数体代码查找全局变量和函数的地方。通常,该环境是定义函数的环境(因此,如果您在提示符下定义它,它将使用 globalenv)。作为“奖励”,它只是为其他对象类型分配 .Environment 属性。这对于大多数对象来说毫无用处,但被公式使用。

第二件事是,它的工作方式几乎与任何其他替换函数类似:如果当前环境中存在该名称,它会直接修改它,否则它会创建一个本地副本并修改它。因此,在您的情况下,它会创建 rect 函数的本地副本并更改其环境。原有功能不变。

# showing names replacement behavior
f <- function() {
  names(letters) <- LETTERS
  letters # the local modified copy
}
f() # prints changed letters
letters # unchanged

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.

# showing names replacement behavior
f <- function() {
  names(letters) <- LETTERS
  letters # the local modified copy
}
f() # prints changed letters
letters # unchanged
痴骨ら 2024-12-29 19:39:51

在您询问并执行的函数内:

 # 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()

因此,您实际上并未对函数中名为“abcde”的 myVal 对象执行任何操作。您而是在函数环境中创建了一个名为“abcede”的新环境。然后执行:

assign( "myVal" , myVal , envir = globalenv() )

它创建了一个名为“myVal”的变量,其字符模式值为“abcde”,该值是从本地函数环境中收集的,并将其放入全局环境中。它现在有一个名为“.Environment”的属性。然而,您的目标是什么仍然不清楚,因为环境被设计用来定义功能的范围。为数据对象分配环境很奇怪。变量位于环境中,但设置变量的环境似乎没有什么用处。变量。所以我认为你的问题的答案:……那有什么好处?”应该是“这没有什么好处。”

Inside a function you asked and executed:

 # 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()

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:

assign( "myVal" , myVal , envir = globalenv() )

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文