官术网_书友最值得收藏!

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:

  1. First, we create an x variable, and we then create a tmpfunc function with x+3 as the return:
    >x<- 5
    >tmpfunc<- function(){
    + x + 3
    + }
    >tmpfunc()
    [1] 8
    
  2. We then create a function named parentfunc with a childfunc nested function and see what returns when we call the parentfunc function:
    >x<- 5
    >parentfunc<- function(){
    + x<- 3
    + childfunc<- function(){
    + x
    + }
    + childfunc()
    + }
    >parentfunc()
    [1] 3
    
  3. Next, we create an x string, and then we create a localassign function to modify x within the function:
    > x <- 'string'
    >localassign<- function(x){
    + x <- 5
    + x
    + }
    >localassign(x)
    [1] 5
    >x
    [1] "string"
    
  4. We can also create another globalassign function but reassign the x variable to 5 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"
主站蜘蛛池模板: 周口市| 舟山市| 启东市| 新泰市| 天等县| 清新县| 新泰市| 射阳县| 凤阳县| 灵山县| 武城县| 邓州市| 额济纳旗| 资阳市| 蒙阴县| 民丰县| 红安县| 西藏| 南通市| 奎屯市| 全椒县| 桃源县| 石林| 敖汉旗| 平原县| 腾冲县| 镇安县| 柏乡县| 日照市| 交口县| 咸丰县| 东平县| 永春县| 栾川县| 临澧县| 滨海县| 公主岭市| 修水县| 洪江市| 临邑县| 广平县|