The sink()
function in R is used to redirect R output to a file, a connection or a function. This can be useful for capturing results, errors or warnings generated from R code for further processing or review.
Here's an example of how to use the sink()
function to redirect R output to a file:
main.r314 chars13 lines
After running the above code, the output generated from the cat()
functions will be saved to a file called "output.txt" in the working directory.
In addition to files, sink()
can also redirect output to a connection or a function by passing in a connection object or a function name as the file
argument.
Note that when using sink()
, it is important to remember to use sink()
a second time, without an argument, to restore output back to the console when you are finished redirecting it. This is necessary because output redirection persists beyond the point where you no longer want it to apply, and this can cause unexpected issues if not handled properly.
gistlibby LogSnag