Re: [R] how to determine if a variable is already set?

2009-09-14 Thread carol white
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

Re: [R] how to determine if a variable is already set?

2009-09-14 Thread Henrique Dallazuanna
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

Re: [R] how to determine if a variable is already set?

2009-09-14 Thread Bert Gunter
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)

Re: [R] how to determine if a variable is already set?

2009-09-14 Thread Henrik Bengtsson
Not the most beautiful solution: start your message with carriage return, e.g. stop(\rA long enough message); A long enough message stop(\rfoo); fooor: /H On Mon, Sep 14, 2009 at 5:06 PM, Bert Gunter gunter.ber...@gene.com wrote: I don't believe the solution proposed below works and anyway

Re: [R] how to determine if a variable is already set?

2009-09-13 Thread Michael Knudsen
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

Re: [R] how to determine if a variable is already set?

2009-09-12 Thread carol white
Thanks for your replies. I use the following script: if(!exists(i)) stop (set the variable i, call. = FALSE) but before the stop expression, Error gets displayed: Error: set the variable i Is there another function that stops the execution, prints an expression without printing Error or any

Re: [R] how to determine if a variable is already set?

2009-09-12 Thread Don MacQueen
Do watch out, however, for *where* i exits. That is, if you type search() you will see a list of environments in which i might be found. You're probably assuming that i, if exists(i) is true, is in .GlobalEnv, but it might be in one of the other environments, in which case exists('i') will be

[R] how to determine if a variable is already set?

2009-09-11 Thread carol white
Hi, It might be a primitive question but how it is possible to determine if a variable is initialized in an environment? Suppose that we start a R session and wants to run a script which use the variable i. Which function could evaluate if i is already initialized or not and if not, then ask

Re: [R] how to determine if a variable is already set?

2009-09-11 Thread Marc Schwartz
On Sep 11, 2009, at 12:15 PM, carol white wrote: Hi, It might be a primitive question but how it is possible to determine if a variable is initialized in an environment? Suppose that we start a R session and wants to run a script which use the variable i. Which function could evaluate if

Re: [R] how to determine if a variable is already set?

2009-09-11 Thread Duncan Murdoch
On 9/11/2009 1:15 PM, carol white wrote: Hi, It might be a primitive question but how it is possible to determine if a variable is initialized in an environment? Suppose that we start a R session and wants to run a script which use the variable i. Which function could evaluate if i is already