The function captures both output and expected messages from an R expression. If the expression results in an unexpected message, an error is issued.
capture_output(expr, srcref = NULL, expected_cnds = NULL, env = caller_env())
A character vector of captured output and messages
An R expression to evaluate
An unquoted R expression
none
The source reference of the expression
NULL
A character vector of expected conditions
If the expression issues a condition of a class that is in this vector, the condition is ignored but added to the return value.
Otherwise, an error is issued.
NULL
The environment in which to evaluate the expression
caller_env()
capture_output(1 + 1)
#> [1] "[1] 2"capture_output(log(-1))
#> Error in capture_output(log(-1)) : The expression
#> > log(-1)
#> issued an unexpected condition:
#> NaNs produced
#> If this is expected, add any of the classes "simpleWarning", "warning", and
#> "condition" to the argument `expected_cnds`.
capture_output(log(-1), expected_cnds = "warning")
#> [1] "[1] NaN" "Warning in log(-1) : NaNs produced"