Send S-PLUS Output to a File

DESCRIPTION:
Directs the output from S-PLUS commands into a file rather than to the terminal.

USAGE:
sink(file=<<see below>>, command=<<see below>>, append = F,
        on.exit = expression(), unsink.to)
sink.number()

OPTIONAL ARGUMENTS:
file:
character string giving the name of a file.
command:
character string giving the name of a command. This is not used if file is given.
append:
logical flag used when writing to a file: if TRUE, output is appended to the end of the file; otherwise the file, if it exists, is overwritten.
on.exit:
An expression to be evaluated when the sink is terminated. It should refer only to global variables, as it will be executed in a different frame than the one present when it is installed.
unsink.to:
A nonnegative integer telling sink to remove sinks until the number of sinks in effect is unsink.to.

VALUE:
When a sink is established sink returns the number of sinks in effect prior to establishing this one. When a sink is terminated and there was an on.exit expression to evaluate, sink returns the value of that expression, otherwise it returns NULL.

sink.number returns the number of sinks in effect.


SIDE EFFECTS:
If file is given, output is diverted to the named file; any previous contents of file are overwritten unless append=TRUE. Otherwise, if command is given, output is diverted through the named command.

If both file and command are omitted, a diversion is ended.


DETAILS:
When output is being diverted to a sink file, nothing appears on the terminal except prompt characters, error messages, and your input.

Sink files can be nested, i.e., if sink is called when output is being diverted to a sink file, the previous diversion is suspended and then resumed when the nested sink is terminated by calling sink() with no arguments.

To produce a file with both the input and the output, use the UNIX command script, if available. Such a script file may need editing to remove control characters.


SEE ALSO:
AUDIT, BATCH, source .

EXAMPLES:
sink("stem.dat")     # divert output to file
stem(data)
sink()  # revert output to the terminal
!lpr stem.dat
# To write a function that puts some output into a file and make
# sure the sink is closed, even if there is an error in the function,
# use on.exit as follows
f <- function(file) {
        old.sink.level <- sink(file)
        on.exit(sink(unsink.to=old.sink.level))
        print(lm(Price~Supply))
}