>t<-4
> f(t)
[1] 13
[1] 120
>w
[1] 12
>t
[1] 4
So,wat the top level did not change, even though it appeared to change
withinf(). Only a localcopyofw, withinf(), changed. Similarly, the top-level
variabletdidn’t change, even though its associated formal argumentydid
change.
NOTE More precisely, references to the localwactually go to the same memory location as the
global one, until the value of the local changes. In that case, a new memory location
is used.
An important exception to this read-only nature of globals arises with
the superassignment operator, which we’ll discuss later in Section 7.8.1.
7.6.5 Extended Example: A Function to Display the Contents of a Call Frame
In single-stepping through your code in a debugging setting, you often want
to know the values of the local variables in your current function. You may
also want to know the values of the locals in the parent function—that is,
the one from which the current function was called. Here, we will develop
code to display these values, thereby further demonstrating access to the
environment hierarchy. (The code is adapted from myedtdbgdebugging
tool in R’s CRAN code repository.)
For example, consider the following code:
f <- function() {
a<-1
return(g(a)+a)
}
g <- function(aa) {
b<-2
aab <- h(aa+b)
return(aab)
}
h <- function(aaa) {
c<-3
return(aaa+c)
}
R Programming Structures 157