Learn R Programming

svMisc (version 0.9-60)

captureAll: 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

captureAll(expr, split = FALSE, file = NULL)

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?
file
a file, or a valid opened connection where output is sinked. 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 is

Value

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

concept

capturing output for GUI clients

See Also

parseText, parse, expression, capture.output, sourceClipboard

Examples

Run this code
writeLines(captureAll(expression(1+1)))
writeLines(captureAll(expression(1+1), split = TRUE))
writeLines(captureAll(parseText("search()")))

writeLines(captureAll(parseText('1:2 + 1:3')))
writeLines(captureAll(parseText("badname")))

## Management of incomplete lines
captRes <- captureAll(parseText("1 +")) # Clearly an incomplete command
if (is.na(captRes)) cat("Incomplete line!
") else writeLines(res)
rm(captRes)

Run the code above in your browser using DataLab