Skip to content

Instantly share code, notes, and snippets.

@mgk
Last active November 13, 2015 19:45
Show Gist options
  • Select an option

  • Save mgk/4f50217cd8ef61e870b9 to your computer and use it in GitHub Desktop.

Select an option

Save mgk/4f50217cd8ef61e870b9 to your computer and use it in GitHub Desktop.
R capture chisq.test() warning
chi_square <- function(x, p) {
# chisq.test() wrapper that captures warning message and invocation string
if (missing(p)) {
expr <- substitute(chisq.test(x), list(x=x))
}
else {
expr <- substitute(chisq.test(x, p=p, rescale.p = TRUE), list(x=x, p=p))
}
res <- tryCatch({
eval(expr)
},
warning=function(w) {
res <- eval(expr)
res$warning <- w$message
res
}
)
res$cmd <- paste0(expr[1], "(", expr[2])
if (!missing(p)) {
res$cmd <- paste0(res$cmd, ", p=", expr[3], ", rescale.p = TRUE")
}
res$cmd <- paste0(res$cmd, ")")
res
}
@mgk
Copy link
Author

mgk commented Nov 13, 2015

The above is unnecessary. The problem has been solved generally. In interactive R do:

> demo(error.catching)

So obvious, not sure how I missed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment