R File output sink and cat

Introduction

It’s often useful to export work into tools such as Excel in order to create nice looking tables or charts. For dataframes the familiar write.csv() function will often suffice, but for other R objects it may be more convenient to output to a flat file such as text or csv.

Data Generation

Let’s generate some example customer data that we can use to explore the file outputs stream functionality in R.

Data <- data.frame(
  CustomerID = 1:1000,
  Gender = sample(c("M", "F", "U", size = 1000, prob = c(0.4, 0.4, 0.1), replace = T)),
  Sales = rnorm(1000, 500, 200)
)

The Cat Function

The cat() function in R (short for concatenate) is helpful for delimited output streams. It operates similar to the familiar c() but allows for specification of “,” or “|” for CSV or PSV files. It is also useful for entering line feed “\n” characters to start a new line.

For example, we can run a crosstabulated count of Gender and separate the results with commas.

### Without file path
cat(xtabs(~Gender, Data), sep = ",")

125,250,125,125,125,125,125

### With File Path
cat(file = "CatTest.csv", xtabs(~Gender, Data), sep = ",")

This function can also take a file to write to as its first parameter which is specified as a string. If that parameter is missing the output will go to the console, or if sink() is active the output will be redirected to the sink destination.

Setting Working Directories

Chances are good that your working directory is set to the R project folder, but this can be changed in the code if not. This saves having to specify a full path name to our destination file.

getwd()
setwd("C:\\ExampleDirectory\MyProject")

The Sink Function

The sink() function allows us to essentially specify a default destination file that R console output will be redirected to. This can be either regular output, the default, or messages such as warnings.

The structure is simple. We specify the file to write the standard output stream to, and close with a call to sink() to close the connection, or empty the sink if you prefer.

Technically exchanging the file path with NULL will close the sink but this is optional as it is the default argument for sink().

There are also two optional parameters for appending or changing the output type of which the defaults are to use console output and overwrite the existing file. These can be left blank.

sink("TestFile.csv", append = F, type = "output")

### Code goes here

sink()

Redirecting Output

Within the call to sink() we can use anything that will output to the console. This includes functions such as table.write() which conveniently allows us to omit the filename and output comma separated tables to the R console, which will then be redirected to our sink.

sink("TestFile.csv", append = F, type = "output")

cat(cat(xtabs(~Gender, Data), sep = ","), "\n")
cat(cat(xtabs(Sales ~ Gender, Data), sep = ","), "\n")
cat(letters[1:3], sep = "\n")
write.table(Data, sep = ",")

sink()
### CSV Output
125,250,125,125,125,125,125 
58673.24,121192.7,62620.31,62368.83,64579.22,62696.59,66427.76 
a
b
c
"CustomerID","Gender","Sales"
"1",1,"1000",137.861941486005
"2",2,"M",143.481060178198

As can be seen from the output we have written two comma separated vectors concatenated by line breaks. Three letters followed by line breaks, and one complete CSV delimited table.

Closing Connections

As an alternative to sink(), we can always obtain a list the standard output connections by specifying all connections in the showConnections() function. Connections can be closed with a simple call to closeAllConnections().

showConnections(all = T)
closeAllConnections()
  description class      mode text   isopen   can read can write
0 "stdin"     "terminal" "r"  "text" "opened" "yes"    "no"     
1 "stdout"    "terminal" "w"  "text" "opened" "no"     "yes"    
2 "stderr"    "terminal" "w"  "text" "opened" "no"     "yes"    

Outputting Summary Data

The sink() function also provides a convenient way to stream summary model output into plain “.txt” files, or even csv.

Below we will reopen our csv and append some linear regression output.

sink("TestFile.csv", append = T, type = "output")

cat("\n", sep = ",")
lm <- lm(Sales ~ Gender, Data)
summary(lm)

sink()
Call:
lm(formula = Sales ~ Gender, data = Data)

Residuals:
    Min      1Q  Median      3Q     Max 
-668.92 -144.55   -3.44  144.63  622.07 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   469.39      18.55  25.305   <2e-16 ***
Gender0.4      15.38      22.72   0.677   0.4984    
...

Writing Data Frames

The write.csv() function provides a convenient interface for writing data frames, similar to write.table(). We can simply leave the file name blank to send the output to our console, and onto the file sink.

sink("TestFile.csv", append = T, type = "output")
cat("\n", sep = ",")

write.csv(Data)

sink()

Conclusion

It can be very convenient to to output text, tables, or other console output into a flat file without having to worry about how to combine the output into into a data frame.

The standard output functions in R provide us with a simple approach when working with unstructured output.