sink
Send Output to a File

Description

Directs the output from commands into a file rather than to the terminal.

Usage

sink(file = NULL, append = FALSE, type = c("output", "message"),
     split = FALSE) 
sink.number(type = c("output", "message"))

Arguments

file a character string giving the name of a file or a valid connection object. Specify NULL to stop sinking.
append a logical flag. If TRUE, if the file exists, output is appended to its end. If FALSE (the default), if the file exists, it is overwritten.
type a character vector with the value "output" or "message". If type is "output", output to stdout() is diverted. If type is "message", output to stderr() is diverted.
split a logical flag. If TRUE, output is sent to the new sink and to the current output stream. If FALSE (the default), the output is not split. When type is "message", split should always be FALSE.

Details

When output is being diverted to a sink file, nothing appears on the terminal except prompt characters, your input, warnings, and error messages.
When messages are being diverted to a sink file, no warnings or error messages appear on the terminal.
Value
sinkreturns NULL.
sink.numberreturns the number of nested calls to sink if type is "output".
sink.numberreturns the connection number currently in use if type is "message". (If messages are not diverted, it returns 2: the stderr() connection.)
Side Effects
Differences between Spotfire Enterprise Runtime for R and Open-source R
In open-source R, the file argument must be an open connection when type="message". In Spotfire Enterprise Runtime for R, it can also be a file name.
See Also
stdout, stderr, BATCH, source.
Examples
sink1 <- tempfile("sink1")
sink2 <- tempfile("sink2")

sink(sink1) # divert output to temp file sink1 1+3 sink(sink2) # divert output to temp file sink2 4+4 sink() # revert output to temp file sink1 5+5 warning("print me") # printed to the terminal sink() # revert output to the terminal readLines(sink1) # [1] "[1] 4" "[1] 10" readLines(sink2) # [1] "[1] 8"

con <- file(sink1, "w") sink(con, type="message") # divert messages to temp file sink1 1+4 # printed to the terminal warning("print me") # diverted to file sink(type="message") # revert messages to the terminal close(con) readLines(sink1) # [1] "Warning message:" "print me"

unlink(c(sink1, sink2)) # clean up

Package base version 6.1.1-7
Package Index