[R] Why does 'exists' need a quoted argument?

Prof Brian D Ripley ripley at stats.ox.ac.uk
Thu Feb 20 17:19:06 CET 2003


foo <- "bar"
exists(foo)

asks if the object whose name is "bar" exists, not that whose name is "foo"
exists.  There is a distinction between operating on objects (rm and is.na)
and names of objects (get, exists, help).

The one exception in your list is help.  That should really be help("rm")
(which works), but help has special semantics (as do library, require and a
few others).


On Thu, 20 Feb 2003, 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?
>
> 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.
>
> I can see that 'get' might need to have the option of using quotes, for example, get("?") or get("*").  However, look at this:
>
> > is.function(?)
> Error: syntax error
> > is.function("?")
> [1] FALSE
>
> I grow used to not quoting things and then stumble over 'exists' from time to time.
>
> Looking forward to clarity or maybe a request for change.

Get used to quoting things ....


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list