warnErrList | R Documentation |
Collect errors (class "error"
, typically from tryCatch
)
from a list x
into a “summary warning”, by default
produce a warning
and keep that message as
"warningMsg"
attribute.
warnErrList(x, warn = TRUE, errValue = NULL)
x |
a |
warn |
logical indicating if |
errValue |
the value with which errors should be replaced. |
a list
of the same length and names as the x
argument, with the error components replaced by errValue
,
NULL
by default, and summarized in the
"warningMsg"
attribute.
The warnErrList()
utility has been used in
lmList()
and nlsList()
in
recommended package nlme forever.
## Regression for each Chick: ChWtgrps <- split(ChickWeight, ChickWeight[,"Chick"]) sapply(ChWtgrps, nrow)# typically 12 obs. nlis1 <- lapply(ChWtgrps, function(DAT) tryCatch(error = identity, lm(weight ~ (Time + I(Time^2)) * Diet, data = DAT))) nl1 <- warnErrList(nlis1) #-> warning : ## 50 times the same error (as Diet has only one level in each group) stopifnot(sapply(nl1, is.null)) ## all errors --> all replaced by NULL nlis2 <- lapply(ChWtgrps, function(DAT) tryCatch(error = identity, lm(weight ~ Time + I(Time^2), data = DAT))) nl2 <- warnErrList(nlis2) stopifnot(identical(nl2, nlis2)) # because there was *no* error at all nlis3 <- lapply(ChWtgrps, function(DAT) tryCatch(error = identity, lm(weight ~ poly(Time, 3), data = DAT))) nl3 <- warnErrList(nlis3) # 1 error caught: stopifnot(inherits(nlis3[[1]], "error") , identical(nl3[-1], nlis3[-1]) , is.null(nl3[[1]]) ) ## With different error messages if(requireNamespace("nlme")) { # almost always, as it is recommended data(Soybean, package="nlme") attr(Soybean, "formula") #-> weight ~ Time | Plot => split by "Plot": L <- lapply(split(Soybean, Soybean[,"Plot"]), function(DD) tryCatch(error = identity, nls(weight ~ SSlogis(Time, Asym, xmid, scal), data = DD))) Lw <- warnErrList(L) } # if <nlme>
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.