[R] Capture warnings

Nitin Jain nitin.jain at bms.com
Tue Feb 19 19:41:01 CET 2008


Dear R-help members,

I am using logistic regression on a high throughput data and would like 
to capture warning messages, if generated for a particular probe.  The 
way I am approaching it currently is:

myResult <- data.frame(matrix(NA, nrow = 1000, ncol = 4))
colnames(myResult) <- c("intercept", "coef", "SE", "Warnings")
myResult[, "Warnings"] <- ""  ## initially set each warning as blank

for (i in seq(nrow(myResult))) {

options(warn = 2) ## Convert warnings to error
probeData <- origData[i, ]  ## get current probe data

fit1 <- try(glm(y ~ x, probeData)) ## fit logistic regression

if (inherits(fit1, "try-error")) { ## change the warn option to 0 and refit
  options(warn = 0)
  fit1 <- try(glm(y ~ x, probeData))
  myResult[i, "Warnings"] <- names(last.warning)[1]
}

## Fill in the other columns of myResult
...
}


When I run the above code in batch mode, I get the error:
Error in x[[jj]][iseq] <- vjj : incompatible types (from NULL to 
character) in subassignment type fix

I think the error is due to the fact that when the first warning message 
is generated, last.warning still does not exist in the function (but in 
some other environment), hence R is trying to assign NULL to a character.

Can anyone tell me how to fix the above problem?

Thanks.

Best,
Nitin




More information about the R-help mailing list