Responsive Ad Area

Share This Post

test

When is assign() appropriate?

I often see questions from novice R programmers where they’ve naively used assign to create multiple objects, and then run into trouble trying to manipulate those objects for a subsequent task (a recent example).

assign appeals to novice users because it has dynamic properties (programmatically creating variable names, in addition to the variable’s values), and seems to mimic some properties of global assignment. Of course, more experienced R programmers come to realize that assign creates code that is hard to read, fragile to maintain, and acts via the type of side effects that are otherwise staunchly avoided in the highly functional R language.

Every question I’ve seen on SO where the OP initially uses assign ultimately has a better answer in lightly refactoring the code to use or produce named vectors, lists, or data frames, usually in tandem with the lapply family of functions. The resulting code is easier to follow, more robust to change, and often more performant.

All this is to say, it’s easy to find examples of why assign is bad. My question is: in what situations would the use of assign be the appropriate, preferred, or only solution?


When is assign() appropriate?
When is assign() appropriate?
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar