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=1of the functionassignrefers 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..GlobalEnvis a variable standing for the global environment andglobalenv()returns the global environment,pos=1is equivalent to saypos=.GlobalEnvorpos=globalenv(). If you want to create a global variable in a function, you can use the functionassignby specifyingpos=1(orpos=.GlobalEnvorpos=globalenv). Another easier but less powerful way is to the<<-or->>to assign/create global variables. -
The function
new.envcreates 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
emptyenvreturns an immutable empty environment. The empty environment is not used to evaluate expressions or run code but rather to check whether nested environments end.