Environments in R
Posted on Nov 22, 2012 in Computer Science
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
** Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives. **
-
The option
pos=1
of the functionassign
refers to the global environment, i.e., the user's workspace. Containers in R are 1-based, so it is easy to understand (and remember) that R use 1 to stand for the global enviroment (user's workspace). Notice that..GlobalEnv
is a variable standing for the global environment andglobalenv()
returns the global environment,pos=1
is equivalent to saypos=.GlobalEnv
orpos=globalenv()
. If you want to create a global variable in a function, you can use the functionassign
by specifyingpos=1
(orpos=.GlobalEnv
orpos=globalenv
). Another easier but less powerful way is to the<<-
or->>
to assign/create global variables. -
The function
new.env
creates a new environment. If you want to source in some code but would rather let the code in a new environment (so that the environment sourcing the code is not contaminated), you can usesource(path_to_file, local=new.env())
. -
The function
emptyenv
returns an immutable empty environment. The empty environment is not used to evaluate expressions or run code but rather to check whether nested environments end.