[R] Testing if an object exists

Prof Brian D Ripley ripley at stats.ox.ac.uk
Sun Jan 27 08:36:22 CET 2002


On Sun, 27 Jan 2002 Gavin.Kennedy at csiro.au wrote:

> Is it possible to test if an object exists without using the object name?
>
> For example I have a function with scanid as a variable. I want to test for
> the attributes of that object, but if the object has
> not already been defined I will get an error 'Object "SC000027" not found'
> where SC000027 is the scanid. So can I
> check if the object exists or not before attempting any operations on it. I
> know I can use exists() but I am want to avoid
> having to pass the object name as an argument.

How else do you intend to specify the object?  If you want to avoid putting
the name in a charcter string as e.g. library does, look at what such
functions do, as in

testObject <- function(object)
{
   exists(as.character(substitute(object)))
}

-- 
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

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list