assign(...,envir = ...)和environment(...)=之间的区别

时间:2021-10-15 23:23:11

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

2 个解决方案

#1


6  

The assign function simply binds a name to a value in the specified environment.

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

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.

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

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.

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

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

#2


1  

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:

所以你没有对函数中名为“abcde”的myVal对象做任何事情。您改为在函数环境中创建了一个名为“abcede”的新环境。然后你执行了:

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

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

#1


6  

The assign function simply binds a name to a value in the specified environment.

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

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.

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

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.

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

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

#2


1  

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:

所以你没有对函数中名为“abcde”的myVal对象做任何事情。您改为在函数环境中创建了一个名为“abcede”的新环境。然后你执行了:

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

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