(Section 3.5)
You use an identifier in a program, e.g.:
Or maybe:
How does R figure out:
a
?as.numeric()
does?c()
does?Scoping
The process by which the computer looks up the object associated with a name in an expression.
Scoping is managed by R’s many environments.
Environment
An object stored in the computer’s memory that keeps track of name-value pairs.
An environment is like a bag of names.
Actually, it’s a collection of names along with the objects to which they are bound.
In addition, each environment also comes with a link to another environment that is its parent. (But we’ll get to that later.)
Active Environment
The environment that R will consult first in order to find the value of any name in an expression.
At any given moment in an R session, some environment plays the role of the active environment.
Global Environment
The environment that is active when one is using R from the console.
This is the environment that you are most often “in”, at this early stage in your use of R.
You can get a list of all the names in the Global Environment:
You can also remove all of the names from your Global Environment:
Parent Environment
The second environment (after the active environment) that R will search when it needs to look up a name.
When R needs to look up a name, it will first search the active environment.
If it doesn’t find the name there, then it follows the link to the parent environment.
But that environment has a parent, too, where R can search.
Search Path
The sequence of environments that the computer will consult in order to find an object associated with a name in an expression.
The sequence begins with the active environment, followed by its parent environment, followed by the parent of the parent environment, and so on.
Define a variable:
Then use it in some code:
In order to execute the previous command:
R had to look up two names:
quadlingColor
cat
The find()
function can tell us!
quadlingColor
in the first place it looked.cat
R had to go pretty far up the Search Path!Run this code:
Now R knows about two things named cat
:
cat()
function in package baseWhat do you think will happen if you run the following command?
Execute this code:
Now what do you think will happen when you run the following?
We can still get to the old cat()
function, but we have to give R a hint where to look. Run this code:
Best not to hide base::cat()
, so let’s remove our cat()
:
Try this:
Now a
and b
and f()
are in the Global Environment:
a
is bound to 10, b
is bound to 4f
is bound to the function we definedf()
Now call f()
:
What did you get for a
, b
, x
and y
? Why?
Run-time Environment (also called the “Evaluation Environment”)
A special environment that is created when a function is called and ceases to exist when the function finishes executing.
The parent environment of the run-time environment is the environment in which the function was defined.
When you have defined a function in your Global Environment, then the Global Environment is the parent environment when the function is called!
f()
could find b
and learn that it was 4.a
to 5 in the run-time environment, so when it looked for the name a
it found it in the run-time environment.Now that you have run f()
, what should a
be: 10 or 5? Why?
Now check: