Emulating dynamic scoping in GNU R

July 21, 2012

(This article was first published on R snippets, and kindly contributed to R-bloggers)

By design GNU R uses lexical scoping. Fortunately it allows for at least two ways to simulate dynamic scoping.

Let us start with the example code and next analyze it:

x <- “global”
f1 <- function() cat(“f1:”, x, “\n”)
f2 <- function() cat(“f2:”, evalq(x, parent.frame()), “\n”)
fun <- function() {
    x <- “f1″
    environment(f1) <- environment()

The difference between functions f1 and f2 is the following. In f1 standard lexical scoping to find x will be usedIn f2 evaluation of x is done in parent environment in the calling stack. This could be called a one level dynamic scoping, because in parent environment x is found using lexical scoping. If they are both called from global environment their behavior is identical.

> f1()

f1: global
> f2()
f2: global

However if they are called from within a function the results will differ:

> fun()
f1: global
f2: f1
f1: f1

We can see that f1 selects a variable from its lexical scope (global environment) and f2 from calling function fun.

An interesting thing is that function’s f1 lexical scope can be changed by assigning new environment to function f1 within function fun. This forces fun environment into lexical search path of f1 and is another way to simulate one level dynamic scoping.

The second method is useful when a function we want to call is defined externally (for example within a package) and we are unable to change it. The drawback is that called function may stop working because within its internals it might call functions or seek variables that are undefined within changed environment search path – so one has to be cautious with it.

In my next post I plan show an application of this idea on some practical example.

To leave a comment for the author, please follow the link and comment on their blog: R snippets.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)