[Rd] How can I catch errors thrown from c via the Rcpp error() function?

Kieran O'Neill koneill at bccrc.ca
Thu Apr 16 03:03:38 CEST 2009


Hi

I am using the flowClust package from BioConductor, which is largely 
implemented in c. For some of my data, the package occasionally (and 
quite stochastically) encounters a particular condition which halts its 
operation. At this point, it calls the error() function defined by Rcpp, 
and halts.

What I would like to be able to do is to catch the error thrown, and 
retry the operation a few times before giving up.

However, when I wrap the call to flowClust in try() or tryCatch(), the 
error seems to completely bypass them:

Examples:

1. This is a trivial example just to test the try() function, and 
correctly assigns the error to the variable x:

 > x <- try(stop(simpleError('blah')))
Error : blah
 > x
[1] "Error : blah\n"
attr(,"class")
[1] "try-error"

2. This is an example using flowClust (using real data, set up to 
guarantee that the error is thrown):

 > x <- try(res30 = flowClust(tFrame, K=30, B=1000, varNames=c('CD4', 
'CD8','KI67', 'CD45RO', 'CD28', 'CD57', 'CCR5', 'CD19', 'CD27', 'CCR7', 
'CD127')))
Error in flowClust(tFrame, K = 30, B = 1000, varNames = c("CD4", "CD8",  :

The covariance matrix is near singular!
Try running the program with a different initial configuration or less 
clusters
 > x
Error: object "x" not found


The c code throwing the error is as follows (from flowClust.c):

if(status!=0)
   {
       error("\n The covariance matrix is near singular! \n Try running 
the program with a different initial configuration or less clusters 
\n");          }


I looked up the error() function in Writing R Extensions  and it states: 
"The basic error handling routines are the equivalents of stop and 
warning in R code, and use the same interface."

Yet, it seems that they are not caught by R's error handling code.

So:

1. Is this the general case (that Rcpp error()s are not handled by try() 
and related methods in R)? (I'm sure this could be tested with a trivial 
example, but I'm not yet familiar enough with wrapping c code in R to do 
so.)

2. If so, what is the correct way to handle them in R?

3. If not, do you have any suggestions as to what may have caused 
flowClust to behave in this way? (So that I can contact the package 
maintainers and report the bug.)



More information about the R-devel mailing list