A Diversion on Binding Values to Symbol

When R tries to bind a value to a symbol,it searches through a series of environments to find the appropriate value.When you are working on the command line and need to retrieve the value of an Robject, the order is roughly

1. Search the global environment for asymbol name matching the one requested.

2. Search the namespaces of each of thepackages on the search list

The search list can be found by using thesearch function.

>search() [1] ".GlobalEnv" "package:stats""package:graphics" [4] "package:grDevices""package:utils" "package:datasets" [7]"package:methods" "Autoloads" "package:base"

Binding Values to Symbol

The global environment or the user’sworkspace is always the first element of the search list and

the base package is always the last.

The order of the packages on the searchlist matters!

User’s can configure which packages getloaded on startup so you cannot assume that there will

be a set list of packages available.

When a user loads a package with librarythe namespace of that package gets put in position

2 of the search list (by default) andeverything else gets shifted down the list.

Note that R has separate namespaces forfunctions and non-functions so it’s possible to have an

object named c and a function named c.

Scoping Rules

The scoping rules for R are the mainfeature that make it different from the original S language. The scoping rulesdetermine how a value is associated with a free variable in a function

R uses lexical scoping or static scoping. Acommon alternative is dynamic scoping.

Related to the scoping rules is how R usesthe search list to bind a value to a symbol

Lexical scoping turns out to beparticularly useful for simplifying statistical computations

Lexical Scoping

Consider the following function.

f<- function(x, y) {

x^2 + y / z

}

This function has 2 formal arguments x andy. In the body of the function there is another symbol z. In this case z iscalled a free variable. The scoping rules of a language determine how valuesare assigned to free variables. Free variables are not formal arguments and arenot local variables (assigned insided the function body).

Lexical scoping in R means that:

the values of free variables are searchedfor in the environment in which the function was defined.

What is an environment?

An environment is a collection of (symbol,value) pairs, i.e. x is a symbol and 3.14 might be its

value.

Every environment has a parent environment;it is possible for an environment to have multiple

“children”

the only environment without a parent isthe empty environment

A function + an environment = a closure orfunction closure

Searching for the value for a freevariable:

If the value of a symbol is not found inthe environment in which a function was defined, then the

search is continued in the parentenvironment.

The search continues down the sequence ofparent environments until we hit the top-level

environment; this usually the globalenvironment (workspace) or the namespace of a package.

After the top-level environment, the searchcontinues down the search list until we hit the empty

environment. If a value for a given symbolcannot be found once the empty environment is

arrived at, then an error is thrown.

Why does all this matter?

Typically, a function is defined in theglobal environment, so that the values of free variables are

just found in the user’s workspac

This behavior is logical for most peopleand is usually the “right thing” to do

However, in R you can have functionsdefined inside other functions

-Languages like C don’t let you do this

Now things get interesting — In this casethe environment in which a function is defined is the

body of another function!

make.power<- function(n) {

pow <- function(x) {

x^n

}

pow

}

This function returns another function asits value

>cube <- make.power(3)

>square <- make.power(2)

>cube(3)

[1]27

>square(3)

[1] 9

Exploring a Function Closure

What’s in a function’s environment?

>ls(environment(cube))

[1]"n" "pow"

>get("n", environment(cube))

[1] 3

>ls(environment(square))

[1]"n" "pow"

>get("n", environment(square))

[1] 2

Lexical vs. Dynamic Scoping

y<- 10

f<- function(x) {

y <- 2

y^2 + g(x)

}

g<- function(x) {

x*y

}

With lexical scoping the value of y in thefunction g is looked up in the environment in which the

function was defined, in this case theglobal environment, so the value of y is 10.

With dynamic scoping, the value of y islooked up in the environment from which the function was

called (sometimes referred to as thecalling environment).

- InR the calling environment

So the value of y would be 2.

When a function is defined in the globalenvironment and is subsequently called from the global environment, then thedefining environment and the calling environment are the same. This cansometimes give the appearance of dynamic scoping.

>g <- function(x) {

+ a<- 3

+x+a+y

+ }

>g(2)

Errorin g(2) : object "y" not found

>y <- 3

>g(2)

[1] 8

Consequences of Lexical Scoping

In R, all objects must be stored in memory

All functions must carry a pointer to theirrespective defining environments, which could be

anywhere

In S-PLUS, free variables are always lookedup in the global workspace, so everything can be

stored on the disk because the “definingenvironment” of all functions is the same.

最新文章

  1. Hibernate的面试题
  2. java多线程的几种实现方式记录
  3. 【Java】一次SpringMVC+ Mybatis 配置多数据源经历
  4. jquery中datagrid中getSelected和getSelections的应用
  5. android 项目学习随笔十五(ShareSDK开放平台)
  6. reactjs入门到实战(十)----one-first_app
  7. S1 : 递归
  8. [tty与uart]1.Linux中tty框架与uart框架之间的调用关系剖析
  9. 简约的单页应用引擎:sonnyJS
  10. Android实现Filterable通过输入文本框实现联系人自动筛选
  11. C++:析构函数
  12. XML学习笔记(2)--dom4j操作XML
  13. java 自定义注解以及获得注解的值
  14. 借贷宝注册送现金疯转 新闻PS图背后真相
  15. OpenMp高速分拣
  16. 【剑指offer】面试题39:深度二叉树
  17. FaceRank,最有趣的 TensorFlow 入门实战项目
  18. c/c++ 网络编程 陈硕老师视频理解之ttcp
  19. 去掉字符空格js
  20. switchyomega插件的基本使用

热门文章

  1. 零基础学python-5.1 数字简单介绍
  2. 从头认识java-15.1 填充容器(3)-填充Map
  3. extjs grid 列顺序紊乱问题
  4. Django框架之ORM
  5. C 项目案例实践(1)数据结构之链表(0)
  6. casperjs userAgent的一些问题
  7. 并不对劲的bzoj2038:p1494:[国家集训队]小Z的袜子
  8. dropZone 回显图片
  9. pycharm中关于django和buildout的配置
  10. bzoj 2811: [Apio2012]Guard【线段树+贪心】