| capture | R Documentation |
This function evaluates an R expression while capturing its printed output, messages, warnings, and errors. It returns a list containing the result of the evaluation along with all the captured texts.
capture(expr)
expr |
An R expression to evaluate. The expression is captured using
|
The function uses withCallingHandlers() and tryCatch() to capture
side effects of evaluating the expression. Printed output is captured using
capture.output(). Warnings and messages are intercepted and their default
display is suppressed using invokeRestart("muffleWarning") and
invokeRestart("muffleMessage"), respectively. If an error occurs, its
message is stored and NULL is returned as the value.
A list with the following components:
The result of evaluating expr.
A character vector with the printed output produced during evaluation.
A character vector with any messages generated during evaluation.
A character vector with any warnings produced during evaluation.
A character string with the error message if an error occurred; otherwise NULL.
## Not run:
# Example: Capturing output, messages, warnings, and errors
captured <- capture({
print("Hello, world!")
message("This is a message.")
warning("This is a warning.")
42 # Final value returned
})
# Display the captured components
print(captured$output) # Printed output
print(captured$messages) # Messages
print(captured$warnings) # Warnings
print(captured$error) # Error message (if any)
print(captured$value) # The evaluated result (42 in this example)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.