withSink

0th

Percentile

Evaluate an R expression while temporarily diverting output

Evaluate an R expression while temporarily diverting output.

Keywords
programming, IO
Usage
withSink(expr, file, append=FALSE, type=c("output", "message"), envir=parent.frame())
Arguments
expr
The R expression to be evaluated.
file
A writable connection or a character string naming the file to write to.
append
If TRUE, the diverted output is appended to the file, otherwise not.
type
A character string specifying whether to divert output sent to the standard output or the standard error. See sink() for details.
envir
The environment in which the expression should be evaluated.
Details

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.

Value

  • Returns the results of the expression evaluated.

See Also

Internally, sink() is used to divert any output.

Aliases
  • withSink
Examples
# Divert standard output
pathname <- tempfile(fileext=".output.txt")
res <- withSink(file=pathname, {
  print(letters)
})
mcat(readLines(pathname), sep="")


# Divert standard error/messages
pathname <- tempfile(fileext=".message.txt")
res <- withSink(file=pathname, type="message", {
  mprint(LETTERS)
})
mcat(readLines(pathname), sep="")
Documentation reproduced from package R.utils, version 2.0.0, License: LGPL (>= 2.1)

Community examples

Looks like there are no examples yet.