withSink | R Documentation |
Evaluate an R expression while temporarily diverting output.
withSink(expr, file, append=FALSE, type=c("output", "message"), substitute=TRUE,
envir=parent.frame())
expr |
The R expression to be evaluated. |
file |
A writable |
append |
If |
type |
A |
substitute |
If |
envir |
The |
Upon exit (also on errors), this function will close the requested "sink". If additional sinks (of any type) where also opened during the evaluation, those will also be closed with a warning.
Returns the results of the expression evaluated.
Henrik Bengtsson
Internally, sink
() is used to divert any output.
# Divert standard output
pathname <- tempfile(fileext=".output.txt")
res <- withSink(file=pathname, {
print(letters)
})
mcat(readLines(pathname), sep="\n")
# Divert standard error/messages
pathname <- tempfile(fileext=".message.txt")
res <- withSink(file=pathname, type="message", {
mprint(LETTERS)
})
mcat(readLines(pathname), sep="\n")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.