sink(file=<<see below>>, command=<<see below>>, append = F,
on.exit = expression(), unsink.to)
sink.number()
file is given.
TRUE, output is appended
to the end of the file; otherwise the file, if it exists, is overwritten.
sink to remove sinks until the
number of sinks in effect is
unsink.to.
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.
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.
file and
command are omitted, a diversion is ended.
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.
sink("stem.dat") # divert output to file
stem(lottery.payoff)
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))
}