R sink Function


sink() function diverts R output to a connection.

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


file: a writable connection or a character string naming the file to write to, or NULL to stop sink-ing
append: logical. If TRUE, output will be appended to file; otherwise, it will overwrite the contents of file
type: character. Either the output stream or the messages stream
split: logical: if TRUE, output will be sent to the new sink and to the current output stream, like the Unix program tee


sink.number() gets how many diversions are in use.
sink.number(type="message") gets the number of connection currently being used for error messages.

> sink("tp.txt") #writ all output to file tp.txt
> for (i in 1:5) print(i);
> sink() #stop sinking, =sink(NULL)

In the tp.txt, the content is:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5


> sink.number()

[1] 0


> for (i in 1:5) print(i) #no sinking, then print to screen

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5


> unlink("tp.txt") #delete the file tp.txt


endmemo.com © 2024  | Terms of Use | Privacy | Home