[R] Why does 'exists' need a quoted argument?
Morten Sickel
Morten.Sickel at nrpa.no
Thu Feb 20 17:33:03 CET 2003
Kevin Wright wrote:
/snip/
>Can someone explain why 'get' and 'exists' require quoted object names?
>Would it make sense (more consistency) to have these functions check to see
if the
>first argument is a string, and if not, then 'substitute' it?
Intuitively, >'exists' is checking to see if an object exists, not to see if
a character string
>exists. Evidently my intuition is wrong.
Well, if you read the ?exists you'll see that it "Search for an R object of
the given name on the search path". Even though I am absolutely no R
developer, I would guess that it would be not impossible to write a function
that does as you thinks, but, its functionality would be worse than for the
current exists, consider for example:
<code>
sql<-Big_ugly_slow_query
sql2<-some_more_parameters
for (i in c("Foo","Bar","Baz"))
if (!exists(i)){
assign(i,sqlquery(handle,paste(sql1,i,sql2)
}
}
</code>
i.e. if the object doesn't exist, I have to initialize it, if it does exist,
I just keep on using it, I know that the object i exists, so I don't care
about that. On the other hand, if it was automatically substituting what was
found to be a string, it would not be possible to
<code>
if (!exists("String")){String<-"Default value"}
</code>
So, no, I think some useful functionality would be thrown out if exist did
not require a string.
Morten Sickel
More information about the R-help
mailing list