Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between assign( ... , envir = ... ) and environment( ... ) =

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
like image 445
SFun28 Avatar asked Dec 26 '11 15:12

SFun28


2 Answers

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
like image 125
Tommy Avatar answered Sep 23 '22 01:09

Tommy


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

like image 22
IRTFM Avatar answered Sep 24 '22 01:09

IRTFM