The difference between assignment (..., envir = ...) and environment (...) =

What is the difference between assigning a value to a name in an environment and setting up a variable environment? I could not figure it out from the documentation.

eg:

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 
+7
source share
2 answers

The assignment function simply associates a name with a value in the specified environment.

But the function of replacing the environment performs two functions: its main purpose is to change the environment of closing the function. This environment is where the function body code searches for global variables and functions. Typically, this envirionment is where the function was defined (so if you define it in a tooltip, it will use globalenv). As a β€œbonus," it simply assigns the .Environment attribute to other types of objects. This is pretty useless for most objects, but is used by formulas.

Secondly, it works almost like any other replacement function: if a name exists in the current environment, it changes it directly, otherwise a local copy is created and modifies it. Thus, in your case, it creates 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 
+6
source

Inside the function you requested 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 did nothing for the myVal object with the name "abcde" that was in this function. Instead, you created a new environment called "abcede" inside the function environment. Then you do:

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

He created a variable named "myVal" with the character mode value "abcde" compiled from the local function environment, and placed it in the global environment. Now it has an attribute named ".Environment". However, it remains unclear what your goals are, since the environment is designed to determine the scope of functions. Assigning an environment to a data object is just weird. Variables are in environments, but it would seem that there would not be a useful goal when setting up a variable's environment. Therefore, I think the answer to your question is: ... what good is this? "Must be" this is not good. "

+1
source

All Articles