Rearranging definitions in R

[This article was first published on Struggling Through Problems » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I came up with a handy little trick for programming in R.

I like to define a lot of variables all at once without worrying about what order they’re in. The goal would be something like this:

K  = -R2/R1
Wc = 1/(R2*C)
R1 = 4.7e+3
R2 = 4.7e+3
C  = 0.05e-9

But of course that doesn’t work because K can’t refer to R2 until R2 has been defined. Now a nice feature in R (or a horrific one, depending on your tastes) is that default arguments to functions are lazily evaluated and may refer to each other (they can even refer to local variables defined within the function!). So the above may be rewritten

Ckt = (function(
    K  = -R2/R1,
    Wc = 1/(R2*C),
    R1 = 4.7e+3,
    R2 = 4.7e+3,
    C  = 0.05e-9
) environment())()

And the variables can now be accessed as Ckt$K, Ckt$Wc, etc. This is often exactly what I want — it packages up these variables into Ckt so they won’t pollute the global environment. But sometimes I do want them to pollute the global environment, and not just by calling attach(). So how would you do that?

The solution I came up with is the rarely used formals() function. It gets or sets the formal parameters of a function. And I used it to build this friendly little contraption…

define = function(Defs) {
    foo = function() { environment() };
    formals(foo) = Defs;
    Parent = parent.frame();

    Results = foo()

    for (Name in ls(Results)) {
        Parent[[Name]] = Results[[Name]]
    }
}

Now the parallel definitions can be done as

define(alist(
    K  = -R2/R1,
    Wc = 1/(R2*C),
    R1 = 4.7e+3,
    R2 = 4.7e+3,
    C  = 0.05e-9
))

But it seems such a shame to lose the lazy-evaluation with the line “Parent[[Name]] = Results[[Name]]”. (I mean it doesn’t actually matter, but it was lazy up until that point!). Problem is what I wrote next didn’t work:

define = function(Defs) {
    foo = function() { environment() };
    formals(foo) = Defs;
    Parent = parent.frame();

    Results = foo()

    for (Name in ls(Results)) {
        delayedAssign(assign.env=Parent, x=Name, value=Results[[Name]])
    }
}

> print(K)
[1] 4255319

> print(Wc)
[1] 4255319

> print(R1)
[1] 4255319

> print(R2)
[1] 4255319

> print(C)
[1] 4255319

Can you figure out why? It’s very subtle.

Basically for loops (and loops in generally, including, irritatingly, lapply) in R do not create a local environment (aka closure). That means that the loop variable (‘Name’ here) gets overwritten each time. Since “Results[[Name]]” is lazily evaluated, it doesn’t have the value either, so they all take on the value of the last one.

The solution is to create a closure. This gives the final product.

define = function(Defs) {
    foo = function() { environment() };
    formals(foo) = Defs;
    Parent = parent.frame();

    Results = foo()

    for (Name in ls(Results)) (function(Name) {
        delayedAssign(assign.env=Parent, x=Name, value=Results[[Name]])
    }) (Name=Name)
}

To leave a comment for the author, please follow the link and comment on their blog: Struggling Through Problems » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)