[R] programming questions

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Nov 3 19:48:38 CET 2010


On Wed, Nov 3, 2010 at 6:17 PM, ivo welch <ivo.welch at gmail.com> wrote:
> yikes.  this is all my fault.  it was the first thing that I ever
> defined when I started using R.
>
>   is.defined <- function(name) exists(as.character(substitute(name)))
>
> I presume there is something much better...

 You didn't do a good job testing your is.defined :)

 Let's see what happens when you feed it 'nonexisting$garbage'. What
gets passed into 'exists'?

acs=function(name){as.character(substitute(name))}

 > acs(nonexisting$garbage)
[1] "$"           "nonexisting" "garbage"

 - and then your exists test is doing effectively exists("$") which
exists. Hence TRUE.

 What you are getting here is the expression parsed up as a function
call ($) and its args. You'll see this if you do:

 > acs(fix(me))
[1] "fix" "me"

Perhaps you meant to deparse it:

 > acs=function(name){as.character(deparse(substitute(name)))}
 > acs(nonexisting$garbage)
 [1] "nonexisting$garbage"
 > exists(acs(nonexisting$garbage))
 [1] FALSE

But you'd be better off testing list elements with is.null

Barry



More information about the R-help mailing list