Skip to content
Prev 199901 / 398502 Next

source() vs attach()0

On 11/11/2009 10:19 AM, Stefan Zeugner wrote:
I would say both are bad choices.  For both, if you later decide to 
change the name of the argument to test(), you break subtest().
For yours, suppose you want to change this later, and introduce an 
intermediary "mid()".  mid() is called by test(), and mid() calls 
subtest().  Then the foo in test won't get changed.

It's better to avoid non-standard evaluation.  Why not pass foo as an 
argument to subtest, and return it as the value?

That is,

subtest <- function(foo) {
   foo + 1
}

test <- function(foo) {
   foo <- subtest(foo)
   return(foo)
}

This will work, it's easy to modify to introduce an intermediary, it 
reduces the dependence between subtest and test, etc.

Duncan Murdoch