Learn R Programming

svMisc (version 1.2.3)

capture_all: Run an R expression and capture output and messages in a similar way as it would be done at the command line

Description

This function captures results of evaluating one or several R expressions the same way as it would be issued at the prompt in a R console. The result is returned in a character string. Errors, warnings and other conditions are treated as usual, including the delayed display of the warnings if options(warn = 0).

Usage

capture_all(expr, split = TRUE, echo = TRUE, file = NULL, markStdErr = FALSE)

captureAll(expr, split = TRUE, echo = TRUE, file = NULL, markStdErr = FALSE)

warnings2(...)

Arguments

expr

A valid R expression to evaluate (names and calls are also accepted).

split

Do we split output, that is, do we also issue it at the R console too, or do we only capture it silently?

echo

Do we echo each expression in front of the results (like in the console)? In case the expression spans on more than 7 lines, only first and last three lines are echoed, separated by [...].

file

A file, or a valid opened connection where output is sunk. It is closed at the end, and the function returns NULL in this case. If file = NULL (by default), a textConnection() captures the output and it is returned as a character string by the function.

markStdErr

If TRUE, stderr is separated from sddout by STX/ETX characters.

...

Items passed directly to warnings2().

Value

Returns a string with the result of the evaluation done in the user workspace.

See Also

parse(), expression(), capture.output()

Examples

Run this code
# NOT RUN {
writeLines(capture_all(expression(1 + 1), split = FALSE))
writeLines(capture_all(expression(1 + 1), split = TRUE))
writeLines(capture_all(parse_text("search()"), split = FALSE))
# }
# NOT RUN {
writeLines(capture_all(parse_text('1:2 + 1:3'), split = FALSE))
writeLines(capture_all(parse_text("badname"), split = FALSE))
# }
# NOT RUN {
# Management of incomplete lines
capt_res <- capture_all(parse_text("1 +")) # Clearly an incomplete command
if (is.na(capt_res)) cat("Incomplete line!\n") else writeLines(capt_res)
rm(capt_res)
# }

Run the code above in your browser using DataLab