You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
x<<-3.14# variable x in the containing environment (global in this case) is updated to be 3.14
64
-
print(x) # since no local variable ‘x’ exists within function ‘crazy’ R searches the containing environments
64
+
print(x) # since no local variable `x` exists within function ‘crazy’ R searches the containing environments
65
65
{ print(x); # this is to demonstrate the function, not a code block, is the smallest environment in R
66
-
x<-42; print(x) # local variable ‘x’ is declared (created) and assigned the value 42; overrides the variable ‘x’ in
66
+
x<-42; print(x) # local variable `x` is declared (created) and assigned the value 42; overrides the variable `x` in
67
67
} # the containing environment
68
-
print(x) # since local variable ‘x’ now exists within the function there is no need to search the containing
68
+
print(x) # since local variable `x` now exists within the function there is no need to search the containing
69
69
} # environment (global in this case)
70
70
>x<-0
71
71
> crazy()
72
72
3.14
73
73
3.14
74
74
42
75
75
42
76
-
>x# variable ‘x’ outside of the function its updated value after the first statement within function ‘crazy()’
76
+
>x# variable `x` outside of the function its updated value after the first statement within function ‘crazy()’
77
77
[1] 3.14
78
78
```
79
79
80
-
The first two print() statements use the variable ‘x’ in the containing environment, as no local variable ‘x’ exists at the moment, which has been updated from x <- 0 to x <- 3.14 via x <<- 3.14 inside function ‘crazy()’.
80
+
The first two print() statements use the variable `x` in the containing environment, as no local variable `x` exists at the moment, which has been updated from x <- 0 to x <- 3.14 via x <<- 3.14 inside function ‘crazy()’.
81
81
82
-
The third print() statement uses the variable ‘x’ just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second print() statements.
82
+
The third print() statement uses the variable `x` just created by the preceding assignment statement x <- 42 which causes the containing environment not to be searched unlike the first and second print() statements.
83
83
84
-
The fourth print() statement uses the variable ‘x’ which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable ‘x’.
84
+
The fourth print() statement uses the variable `x` which exists within the function because the x <- 42 now masks access, at least for anything other than the super-assignment operator, to the containing environment’s variable `x`.
85
85
86
-
I added a call to variable ‘x’ after the function ‘crazy()’ returns to show it keeps the new value assigned to it by the super-assignment operator inside function ‘crazy()’.
86
+
I added a call to variable `x` after the function ‘crazy()’ returns to show it keeps the new value assigned to it by the super-assignment operator inside function ‘crazy()’.
87
87
88
88
The super-assignment operator does not update a variable of the same name inside an inner function but the innermost environment inherits any changes unless a local variable of the same name exists within the inner function as demonstrated by x <- 42; print(x) and print(x).
89
-
Furthermore, if a variable named ‘x’ had existed inside function ‘crazy()’ and preceded the call to the super-assignment operator, the results would be as shown in the next example.
89
+
Furthermore, if a variable named `x` had existed inside function ‘crazy()’ and preceded the call to the super-assignment operator, the results would be as shown in the next example.
90
90
91
91
```R
92
92
crazy<-function() {
@@ -104,29 +104,29 @@ crazy <- function() {
104
104
```
105
105
106
106
To reiterate the concept the next section explains in more detail the role and behaviour of the “superassignment” operator which allows the programmer to modify a variable declared outside of the current function in which the reference to the variable is made.
107
-
In the simplest example consider how variable ‘x’ changes when the crazy() function is called.
107
+
In the simplest example consider how variable `x` changes when the crazy() function is called.
108
108
109
109
```R
110
110
# Declare and define a function named crazy()
111
-
crazy<-function() { # create a new environment with a local variable ‘x’ and access to another variable ‘x’
111
+
crazy<-function() { # create a new environment with a local variable `x` and access to another variable `x`
112
112
# declared somewhere outside this function
113
-
x<-3.14# assign the numeric value 3.14 to local variable ‘x’
114
-
print(x) # output the current value of local variable ‘x’ (1)
115
-
{ print(x); # output the current value of local variable ‘x’ (2)
116
-
x<<-42; # assign the numeric value 42 to variable ‘x’ declared outside this function (3)
117
-
print(x) # output the current value of local variable ‘x’ (4)
113
+
x<-3.14# assign the numeric value 3.14 to local variable `x`
114
+
print(x) # output the current value of local variable `x` (1)
115
+
{ print(x); # output the current value of local variable `x` (2)
116
+
x<<-42; # assign the numeric value 42 to variable `x` declared outside this function (3)
117
+
print(x) # output the current value of local variable `x` (4)
118
118
}
119
-
print(x) # output the current value of local variable ‘x’ (5)
119
+
print(x) # output the current value of local variable `x` (5)
120
120
}
121
-
>x<-0# Declare and define a local variable named ‘x’
122
-
>x# output the current value of local variable ‘x’
121
+
>x<-0# Declare and define a local variable named `x`
122
+
>x# output the current value of local variable `x`
123
123
0
124
124
> crazy() # Call function crazy()
125
-
3.14# (1) inner variable ‘x’
126
-
3.14# (2) inner variable ‘x’
127
-
3.14# (4) inner variable ‘x’
128
-
3.14# (5) inner variable ‘x’
129
-
>x# (3) containing environment variable ‘x’
125
+
3.14# (1) inner variable `x`
126
+
3.14# (2) inner variable `x`
127
+
3.14# (4) inner variable `x`
128
+
3.14# (5) inner variable `x`
129
+
>x# (3) containing environment variable `x`
130
130
42
131
131
```
132
132
@@ -139,8 +139,8 @@ x <- 3.14
139
139
is treated as though it is as shown below.
140
140
141
141
```R
142
-
x<-3.24# assigns the value 3.14 to local variable ‘x’ not the variable ‘x’ in the containing environment
143
-
x<<-42# assigns the value 42 to variable ‘x’ in the containing environment
142
+
x<-3.24# assigns the value 3.14 to local variable `x` not the variable `x` in the containing environment
143
+
x<<-42# assigns the value 42 to variable `x` in the containing environment
144
144
```
145
145
146
146
Perhaps the crude graphic can illuminate the effects of lexical scoping on variables a better than mere words.
0 commit comments