capture.output | R Documentation |
Evaluates its arguments with the output being returned as a character
string or sent to a file. Related to sink
similarly to how
with
is related to attach
.
capture.output(..., file = NULL, append = FALSE, type = c("output", "message"), split = FALSE)
... |
Expressions to be evaluated. |
file |
A file name or a connection, or |
append |
logical. If |
type, split |
are passed to |
It works via sink(<file connection>)
and hence the R code
in dots
must not interfere with the connection (e.g., by
calling closeAllConnections()
).
An attempt is made to write output as far as possible to file
if there is an error in evaluating the expressions, but for
file = NULL
all output will be lost.
Messages sent to stderr()
(including those from
message
, warning
and stop
)
are captured by type = "message"
. Note that this can be
“unsafe” and should only be used with care.
A character string (if file = NULL
), or invisible NULL
.
sink
, textConnection
require(stats) glmout <- capture.output(summary(glm(case ~ spontaneous+induced, data = infert, family = binomial()))) glmout[1:5] capture.output(1+1, 2+2) capture.output({1+1; 2+2}) ## Not run: ## on Unix-alike with a2ps available op <- options(useFancyQuotes=FALSE) pdf <- pipe("a2ps -o - | ps2pdf - tempout.pdf", "w") capture.output(example(glm), file = pdf) close(pdf); options(op) ; system("evince tempout.pdf &") ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.