I don't believe the solution proposed below works and anyway misses the whole point of tryCatch(), which is **not** to test manually:
f <- function(i){ val <- tryCatch(get("i"), error = function(e)"input error") val ## can test val and carry on if not an error } ## testit > f() [1] "input error" > f(2) [1] 2 > Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Henrique Dallazuanna Sent: Monday, September 14, 2009 12:44 PM To: carol white Cc: r-h...@stat.math.ethz.ch Subject: Re: [R] how to determine if a variable is already set? Try this: if (!exists("i")) tryCatch(stop(""), error = function(e)invisible(), finally=print("Please set the i variable")) On Mon, Sep 14, 2009 at 3:38 PM, carol white <wht_...@yahoo.com> wrote: > Thanks to your replies. > > In fact, the problem doesn't come from "exists" but from "stop" that > displays Error even if call. = FALSE. To answer to Dan, I quoted the > pramater of exists. So when the variable is not defined, stop displays the > expression message preceded by "Error". So the question was how not to > display "Error" or if there is another function that does stop's job without > displaying "Error"? > > Cheers, > > --- On Sun, 9/13/09, Michael Knudsen <micknud...@gmail.com> wrote: > > From: Michael Knudsen <micknud...@gmail.com> > Subject: Re: [R] how to determine if a variable is already set? > To: "carol white" <wht_...@yahoo.com> > Cc: r-h...@stat.math.ethz.ch > Date: Sunday, September 13, 2009, 7:06 AM > > On Fri, Sep 11, 2009 at 7:15 PM, carol white <wht_...@yahoo.com> wrote: > > > It might be a primitive question but how it is possible to determine if a > variable is initialized in an environment? > > What about this? > > > "x" %in% ls() > [1] FALSE > > x = 41 > > "x" %in% ls() > [1] TRUE > > Best, > Michael > > -- > Michael Knudsen > micknud...@gmail.com > http://lifeofknudsen.blogspot.com/ > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Henrique Dallazuanna Curitiba-Parana-Brasil 250 25' 40" S 490 16' 22" O [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.