Skip to contents

Execute an expression while redirecting output to a file

Usage

withSink(expr, logfile = tempfile(fileext = ".txt"))

Arguments

expr

The expression to execute

logfile

The file to redirect output to. Default is "tmp.txt".

Value

The result of the expression

Examples

logfile <- tempfile(fileext = ".txt")
withSink(logfile = logfile, expr = {
  cat("Helloworld\n")
  message("Goodbye")
})
#> Goodbye
readLines(logfile) == c("Helloworld", "Goodbye")
#> [1]  TRUE FALSE