Last active
November 13, 2015 19:45
-
-
Save mgk/4f50217cd8ef61e870b9 to your computer and use it in GitHub Desktop.
R capture chisq.test() warning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above is unnecessary. The problem has been solved generally. In interactive R do:
So obvious, not sure how I missed it.