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. Kevin Wright This communication is for use by the intended recipient and cont ... [[dropped]]
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
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
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 seeif 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