What is the purpose of the environment in R and when do I need to use more than one?

This is the main question R: R has the concept of environment. So what is his purpose when I need to run more than one and how do I switch between them? What is the advantage of multiple environments (e.g. finding the contents of a .Rdata file)?

+4
source share
3 answers

The idea of ​​the environment is important, and you use it all the time, mostly without being aware of it. If you just use R and don’t do anything interesting, indirect use of the environment is all you need and you don’t need to explicitly create and manage environments. Only when you switch to more advanced use will you need to understand more. The main place that you use (indirectly) is that each function has its own environment, so every time you run the function, you use new environments. Why is this important because it means that if the function uses a variable named "x" and you have a variable named "x", then the computer can support them directly and use the correct one when necessary, and your copy is "x "msgstr" is not overwritten by the version of functions.

Some other cases where you can use environments: each package has its own environment, so 2 packages can be loaded with the same name of the internal function, and they will not interfere with each other. You can keep your workspace a little cleaner by adding new definitions of the environment and load functions to this environment rather than to the global or work environment. When you write your own functions and want to share variables between functions, you will need to understand the environments. Environmets can be used to emulate pass-by-reference instead of pass-by-value, if you have ever been in a situation where it matters (if you do not recognize these phrases, then it probably does not matter).

+7
source

You can think of environment as an unordered list s. Both types of data offer something like a hash table data structure to the user, i.e. a mapping from names to values. Lack of order in the environment provides better performance compared to list for similar tasks.

The access functions [[ and $ work for both.

The good fact about the environment , which is not true for list , is that the environment is passed by reference when supplied as arguments to the function, offering a way to improve performance when working with large objects.

+6
source

Personally, I never work directly with the environment. Instead, I divide my scripts into functions. This leads to increased reuse and to a larger structure. In addition, each function operates in its own environment, providing minimal interference in variables, etc.

+4
source

Source: https://habr.com/ru/post/1442383/


All Articles