# Usually *values* of objects are copied
x <- 2
y <- x # y gets value of x
x <- 4
y # value of y did not change with x[1] 2
2026-01-29
Now create another reference to e1:
The ‘two’ environments are just two different names for the same thing
.GlobalEnv or global_envcurrent_env()environments implement the lexical scope of a function:
a new environment is created when a function is called, with the function’s parameters and any objects defined in the function
f(1), what is the value of x before f(1) is called, what is it after f(1)?Call the library rlang. What is the parent environment of the global environment?
In the fac function, add print of the current and the parent environment. Then run fac(5).
env_parents returns a list of all parent environments.
<<-<<- is the assignment of a value to an object in the (first) parent environment (in which the value is found).
In a programming setting we might want to have more control, and specify the exact parent environment. Otherwise there might be unexpected side effects when somebody else uses our function.
The implementation is easily breakable: everybody can change counter in the global environment
If we want to count something else (like sqrt calls), we need separate counters
now use it …
[1] 25
[1] 9
[1] 1
With get we get the value of an object