Working with lexical scoping
Lexical scoping, also known as static binding, determines how a value binds to a free variable in a function. This is a key feature that originated from the scheme functional programming language, and it makes R different from S. In the following recipe, we will show you how lexical scoping works in R.
Getting ready
Ensure that you completed the previous recipes by installing R on your operating system.
How to do it...
Perform the following steps to understand how the scoping rule works:
- First, we create an
x
variable, and we then create atmpfunc
function withx+3
as the return:>x<- 5 >tmpfunc<- function(){ + x + 3 + } >tmpfunc() [1] 8
- We then create a function named
parentfunc
with achildfunc
nested function and see what returns when we call theparentfunc
function:>x<- 5 >parentfunc<- function(){ + x<- 3 + childfunc<- function(){ + x + } + childfunc() + } >parentfunc() [1] 3
- Next, we create an
x
string, and then we create alocalassign
function to modifyx
within the function:> x <- 'string' >localassign<- function(x){ + x <- 5 + x + } >localassign(x) [1] 5 >x [1] "string"
- We can also create another
globalassign
function but reassign thex
variable to5
using the<<-
notation:> x <- 'string' >gobalassign<- function(x){ + x <<- 5 + x + } >gobalassign(x) [1] 5 >x [1] 5
How it works...
There are two different types of variable binding methods: one is lexical binding, and the other is dynamic binding. Lexical binding is also called static binding in which every binding scope manages variable names and values in the lexical environment. That is, if a variable is lexically bound, it will search the binding of the nearest lexical environment. In contrast to this, dynamic binding keeps all variables and values in the global state. That is, if a variable is dynamically bound, it will bind to the most recently created variable.
To demonstrate how lexical binding works, we first create an x
variable and assign 5
to x
in the global environment. Then, we can create a function named tmpfunc
. The function outputs x + 3
as the return value. Even though we do not assign any value to x
within the tmpfunc
function, x
can still find the value of x
as 5
in the global environment.
Next, we create another function named parentfunc
. In this function, we assign x
to 3
and create a childfunc
nested function (a function defined within a function). At the bottom of the parentfunc
body, we invoke childfunc
as the function return. Here, we find that the function uses the x
defined in parentfunc
instead of the one defined outside parentfunc
. This is because R searches the global environment for a matched symbol name, and then subsequently searches the namespace of packages on the search list.
Moving on, let's take a look at what will return if we create an x
variable as a string in the global state and assign an x
local variable to 5
within the function. When we invoke the localassign
function, we discover that the function returns 5
instead of the string value. On the other hand, if we print out the value of x
, we still see string in return. While the local variable and global variable have the same name, the assignment of the function does not alter the value of x
in global state. If you need to revise the value of x
in the global state, you can use the <<-
notation instead.
There's more...
In order to examine the search list (or path) of R, you can type search()
to list the search list:
>search() [1] ".GlobalEnv""tools:rstudio" [3] "package:stats" "package:graphics" [5] "package:grDevices" "package:utils" [7] "package:datasets" "package:methods" [9] "Autoloads" "package:base"