It contains one environment for each attached package and any other objects that you’ve attach()ed. This is called the search path because objects in these environments can be found from the top-level interactive workspace. Search() lists all parents of the global environment. The environment() is the current environment. The emptyenv(), or empty environment, is the ultimate ancestor of all environments, and the only environment without a parent. The baseenv(), or base environment, is the environment of the base package. The parent of the global environment is the last package that you attached with library() or require(). This is the environment in which you normally work. The globalenv(), or global environment, is the interactive workspace. This is discussed in more detail in calling environments. Instead, it gives you the calling environment. For example, ame() doesn’t give you the parent frame of an environment. Unfortunately “frame” is used inconsistently in R. More technically, an environment is made up of two components, the frame, which contains the name-object bindings (and behaves much like a named list), and the parent environment. The names in an environment are not ordered (i.e., it doesn’t make sense to ask what the first element of an environment is). Generally, an environment is similar to a list, with four important exceptions: ![]() It’s rare to talk about the children of an environment because there are no back links: given an environment we have no way to find its children. The grandparent of an environment is the parent’s parent, and the ancestors include all parent environments up to the empty environment. We use the metaphor of a family to refer to environments. Only one environment doesn’t have a parent: the empty environment. The parent is used to implement lexical scoping: if a name is not found in an environment, then R will look in its parent (and so on). In diagrams, I’ll represent the pointer to parent with a small black circle. This process is described in more detail in gc.Įvery environment has a parent, another environment. If an object has no names pointing to it, it gets automatically deleted by the garbage collector. You can think of an environment as a bag of names:Įach name points to an object stored elsewhere in memory: The job of an environment is to associate, or bind, a set of names to a set of values. You can install pryr by running install.packages("pryr") Environment basics This chapter uses many functions from the pryr package to pry open R and look inside at the messy details. Recursing over environments provides a function template for computing with environments, illustrating the idea with a useful function.įunction environments revises R’s scoping rules in more depth, showing how they correspond to four types of environment associated with each function.īinding names to values describes the rules that names must follow (and how to bend them), and shows some variations on binding a name to a value.Įxplicit environments discusses three problems where environments are useful data structures in their own right, independent of the role they play in scoping. How do you determine the environment from which a function was called?Įnvironment basics introduces you to the basic properties of an environment and shows you how to create your own. What is the enclosing environment of a function? Why is it important? What is the parent of the global environment? What is the only environment that doesn’t have a parent? List at least three ways that an environment is different to a list. ![]() You can find the answers at the end of the chapter in answers. If you can answer the following questions correctly, you already know the most important topics in this chapter. ![]() Reference semantics are not often needed, but can be extremely useful. When you modify a binding in an environment, the environment is not copied it’s modified in place. This chapter dives deep into environments, describing their structure in depth, and using them to improve your understanding of the four scoping rules described in lexical scoping.Įnvironments can also be useful data structures in their own right because they have reference semantics. The environment is the data structure that powers scoping. You’re reading the first edition of Advanced R for the latest on this topic, see the Environments chapter in the second edition.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |