Re: [R] try-error can not be test. Why?

2010-09-09 Thread Duncan Murdoch

On 08/09/2010 11:46 PM, Philippe Grosjean wrote:

On 08/09/10 19:25, David Winsemius wrote:

On Sep 8, 2010, at 1:18 PM, telm8 wrote:


Hi,

I am having some strange problem with detecting try-error. From what I
have read so far the following statement:


try( log(a) ) == try-error


should yield TRUE, however, it yields FALSE. I can not figure out why.
Can
someone help?


  class(try( log(a), silent=TRUE )) == try-error
[1] TRUE


This is perfectly correct in this case, but while we are mentioning a 
test on the class of an object, the better syntax is:


  inherits(try(log(a)), try-error)

In a more general context, class may be defined with multiple strings (R 
way of subclassing S3 objects). For instance, this does not work:


  if (class(Sys.time()) == POSIXct) ok else not ok

... because the class of a `POSIXct' object is defined as: c(POSIXt, 
POSIXct). This works:


Getting even further off track:  another advantage of inherits() is that 
the class can change.  For example, in the upcoming 2.12.0 release, the 
class of Sys.time() will be


 class(Sys.time())
[1] POSIXct POSIXt

Putting the names in the reverse order was a relic from ancient times 
that will soon be corrected.


The tests below won't care about this change, but some more fragile 
tests might.


Duncan Murdoch



  if (inherits(Sys.time(), POSIXct)) ok else not ok

Alternate valid tests would be (but a little bit less readable):

  if (any(class(Sys.time()) == POSIXct)) ok else not ok

or, by installing the operators package, a less conventional, but 
cleaner code:


  install.packages(operators)
  library(operators)
  if (Sys.time() %of% POSIXct) ok else not ok

Best,

Philippe Grosjean


Many thanks
--
View this message in context:
http://r.789695.n4.nabble.com/try-error-can-not-be-test-Why-tp2531675p2531675.html

Sent from the R help mailing list archive at Nabble.com.

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

David Winsemius, MD
West Hartford, CT

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




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


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


[R] try-error can not be test. Why?

2010-09-08 Thread telm8

Hi,

I am having some strange problem with detecting try-error. From what I
have read so far the following statement:


try( log(a) ) == try-error


should yield TRUE, however, it yields FALSE. I can not figure out why. Can
someone help?

Many thanks
-- 
View this message in context: 
http://r.789695.n4.nabble.com/try-error-can-not-be-test-Why-tp2531675p2531675.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] try-error can not be test. Why?

2010-09-08 Thread David Winsemius


On Sep 8, 2010, at 1:18 PM, telm8 wrote:



Hi,

I am having some strange problem with detecting try-error. From  
what I

have read so far the following statement:


try( log(a) ) == try-error


should yield TRUE, however, it yields FALSE. I can not figure out  
why. Can

someone help?



 class(try( log(a), silent=TRUE )) == try-error
[1] TRUE





Many thanks
--
View this message in context: 
http://r.789695.n4.nabble.com/try-error-can-not-be-test-Why-tp2531675p2531675.html
Sent from the R help mailing list archive at Nabble.com.

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


David Winsemius, MD
West Hartford, CT

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


Re: [R] try-error can not be test. Why?

2010-09-08 Thread telm8

Thank worked! Thank you so much!!!
-- 
View this message in context: 
http://r.789695.n4.nabble.com/try-error-can-not-be-test-Why-tp2531675p2531726.html
Sent from the R help mailing list archive at Nabble.com.

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


Re: [R] try-error can not be test. Why?

2010-09-08 Thread Philippe Grosjean

On 08/09/10 19:25, David Winsemius wrote:


On Sep 8, 2010, at 1:18 PM, telm8 wrote:



Hi,

I am having some strange problem with detecting try-error. From what I
have read so far the following statement:


try( log(a) ) == try-error


should yield TRUE, however, it yields FALSE. I can not figure out why.
Can
someone help?



  class(try( log(a), silent=TRUE )) == try-error
[1] TRUE


This is perfectly correct in this case, but while we are mentioning a 
test on the class of an object, the better syntax is:


 inherits(try(log(a)), try-error)

In a more general context, class may be defined with multiple strings (R 
way of subclassing S3 objects). For instance, this does not work:


 if (class(Sys.time()) == POSIXct) ok else not ok

... because the class of a `POSIXct' object is defined as: c(POSIXt, 
POSIXct). This works:


 if (inherits(Sys.time(), POSIXct)) ok else not ok

Alternate valid tests would be (but a little bit less readable):

 if (any(class(Sys.time()) == POSIXct)) ok else not ok

or, by installing the operators package, a less conventional, but 
cleaner code:


 install.packages(operators)
 library(operators)
 if (Sys.time() %of% POSIXct) ok else not ok

Best,

Philippe Grosjean


Many thanks
--
View this message in context:
http://r.789695.n4.nabble.com/try-error-can-not-be-test-Why-tp2531675p2531675.html

Sent from the R help mailing list archive at Nabble.com.

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


David Winsemius, MD
West Hartford, CT

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




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


Re: [R] try-error can not be test. Why?

2010-09-08 Thread David Winsemius


On Sep 8, 2010, at 11:46 PM, Philippe Grosjean wrote:


On 08/09/10 19:25, David Winsemius wrote:


On Sep 8, 2010, at 1:18 PM, telm8 wrote:



Hi,

I am having some strange problem with detecting try-error. From  
what I

have read so far the following statement:


try( log(a) ) == try-error


should yield TRUE, however, it yields FALSE. I can not figure out  
why.

Can
someone help?



 class(try( log(a), silent=TRUE )) == try-error
[1] TRUE


This is perfectly correct in this case, but while we are mentioning  
a test on the class of an object, the better syntax is:


 inherits(try(log(a)), try-error)

In a more general context, class may be defined with multiple  
strings (R way of subclassing S3 objects). For instance, this does  
not work:


 if (class(Sys.time()) == POSIXct) ok else not ok

... because the class of a `POSIXct' object is defined as:  
c(POSIXt, POSIXct). This works:


 if (inherits(Sys.time(), POSIXct)) ok else not ok

Alternate valid tests would be (but a little bit less readable):

 if (any(class(Sys.time()) == POSIXct)) ok else not ok

or, by installing the operators package, a less conventional, but  
cleaner code:


 install.packages(operators)
 library(operators)
 if (Sys.time() %of% POSIXct) ok else not ok


I have also used if (try-error %in% class(try(log(a))) ) { } else  
{ }, but the inherits() form looks at the very least less clunky.




Best,

Philippe Grosjean


Many thanks
--
View this message in context:
http://r.789695.n4.nabble.com/try-error-can-not-be-test-Why-tp2531675p2531675.html

Sent from the R help mailing list archive at Nabble.com.

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


David Winsemius, MD
West Hartford, CT

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




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