How to use the sink() function function in R

When doing data science, sometimes you need to save your data to a file outside of the R programming language. If you are saving the data so that you can use it in other places, then the sink function may be just what you are looking for. It is a straightforward way of getting the job done.

Description

The sink function is an easy way to save data to an external file. It uses two formats to open and close the file. In between the opening and closing of the file, the normal print statements send the data to the file instead of the console window. The opening format is sink(“filename”, append) where “filename” is the name of the file the data is to be saved to. Note that the file name needs to be in quotes. The “append” argument adds the data to the end of the file if true and overwrites the file if false. The default value for this argument is false. The closing format is sink() and as you can see it has no arguments. What this function does is send the usual output to a file instead of the console window.

Explanation

The sink function works by opening a file and saving any data to it that would normally be sent to the console window. You then close the file by applying the function again without any arguments. You can send any type of data that would normally be sent to the console window. Once saved it is possible to load it into other programs. Unfortunately, when saving it to a CVS file it does not save individual columns so they can be viewed properly in a spreadsheet, however, when saved to a text file the data is displayed exactly as it would be in the console window.

Examples

Here we have five Examples of code that illustrate the sink function in action. Each of them shows it being used in different ways. In all five cases, the basic process is the same, the difference is in the data being saved, the format of the data, and even the file type.

> sink(“test1.txt”)
> “This is a test, it is only a test”
> sink()

Here is a simple example where we create the file name, provide a single line of text, and then close the file.

> sink(“test2.txt”)
> “This is an even bigger test.”
> “it is testing the sink function to create a text file.”
> “Here is a demonstration of the use of this function.”
> “If you run this routine, you will produce a text file with this text in it.”
> sink()

Here is an example where we create the file name, provide four lines of text, and then close the file.

> df = data.frame(Z = c(“A”, “A”,”B”, “B”, “A”, “B”),
+ X = c(“F”, “A”, “E”, “C”, “D”, “B”),
+ Y = c( 44, 22, 33, 29, 31, 16))
> sink(“test3.txt”)
> df
> sink()

Here is an example where we save a data frame to a text file using the sink function. The result is that it reproduces the data frame in the text file.

> df = data.frame(Z = c(“A”, “A”,”B”, “B”, “A”, “B”),
+ X = c(“F”, “A”, “E”, “C”, “D”, “B”),
+ Y = c( 44, 22, 33, 29, 31, 16))
> sink(“test.csv”)
> df
> sink()

In this example, we save a data frame to a CSV file allowing it to be loaded into excel. Unfortunately, it does not separate the individual columns, however, it does separate the rows.

> sink(“test4.txt”)
> “This is a test, it is only a test”
> sink()
> sink(“test4.txt”, append = TRUE)
> “This is an even bigger test.”
> sink()

This example illustrates appending a file, by creating a file and then adding additional data to it.

Application

The main application of the sink function is saving data to external files. This is particularly handy when you want to transfer the data that you have been working with and the result of your analysis to another program. This is particularly helpful When you are preparing to write up a report on your analysis and want to transfer your data to a word processor. It makes it easier to transfer data from R to other programs.

The sink function makes it easy to save data to an external file so that it can be used in other programs. Its main usage involves only one or two easy-to-use arguments. Once called to open a file, sending data to the file uses the same commands as sending it to the console window. It is an easy function to use and understand.

Scroll to top