sink(file = NULL, append = FALSE, type = c("output", "message"), split = FALSE) sink.number(type = c("output", "message"))
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. |
sink | returns NULL. |
sink.number | returns the number of nested calls to sink if type is "output". |
sink.number | returns the connection number currently in use if type is "message". (If messages are not diverted, it returns 2: the stderr() connection.) |
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