[R] Why does 'exists' need a quoted argument?
Jason Turner
jasont at indigoindustrial.co.nz
Thu Feb 20 17:15:03 CET 2003
On Thu, Feb 20, 2003 at 08:52:22AM -0600, Wright, Kevin wrote:
>
> Some functions in R need quoted arguments. Consider this list:
>
> help(rm)
> rm(a)
> is.na(a)
> get("rm")
> exists("rm")
>
> Can someone explain why 'get' and 'exists' require quoted object names?
...
I just tried it:
my.exists <- function (x, where = -1,
envir = if (missing(frame)) as.environment(where)
else sys.frame(frame),
frame, mode = "any", inherits = TRUE) {
if(!is.character(x)) x <- deparse(substitute(x))
.Internal(exists(x, envir, mode, inherits))
}
The problem is the the "if" statement if the object doesn't exist. R
quite correctly stops when it tries to do a conditional based on a
non-existant object.
If, on the other hand, we don't do that, and deparse(substitute(x))
without the check, it does unexpected things if a string is given to
it.
my.exists <- function (x, where = -1,
envir = if (missing(frame)) as.environment(where)
else sys.frame(frame),
frame, mode = "any", inherits = TRUE) {
x <- deparse(substitute(x))
.Internal(exists(x, envir, mode, inherits))
}
> my.exists(ls)
[1] TRUE
> my.exists("ls")
[1] FALSE
> deparse(substitute("ls"))
[1] "\"ls\""
So deparse(substitute(x)) correctly passes the quotes, which wasn't
our intention (but it *was* what we asked for... ;).
If we start pulling quotes out using gsub or similar, we're getting
into very kludgy territory.
Jason
--
Indigo Industrial Controls Ltd.
64-21-343-545
jasont at indigoindustrial.co.nz
More information about the R-help
mailing list