collect_all | R Documentation |
collect_all
wraps expressions and returns the result of the expression along with
a list of warnings, errors, and messages raised by running the expression,
without having to run the expression more than once.
collect_all(expr, catchErrors = FALSE, asStrings = TRUE)
expr |
The expression you want to catch warnings and messages for. |
catchErrors |
A boolean which, if true, will catch error messages just like it catches warnings and messages. It will then return |
asStrings |
A boolean which, if true, will convert the conditions into strings. |
I've personally found R's warning and message handling very confusing, and this represents "good enough" code for me.
Using Aaron's answer to a question on stackexchange,
I was able to understand enough of it to make a function that would collect all the warnings and messages
raised by an expression and still run the code only once. (All other examples I encountered seemed to need to run
the code twice to get both the result and the warnings.)
If, say, you're running a lot of models
all at once, then having to rerun the code (as most tutorials/answers to warning handling with R suggest)
would be a total pain in the butt.
A named list with the result of the expression, the warnings, and the messages raised by the expression
# Let's say that `run_model_once(x)` fits a randomly generated glmer model with # a seed of `x`, as one might do in a power simulation ## Not run: results = data.frame(IterationNumber = seq(NUMBER_ITERATIONS)) results = results %>% dplyr::tbl_df() %>% dplyr::mutate(models = purrr::map(IterationNumber, ~zplyr::collect_all(run_model_once(.)))) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.