5 Input and Output

5.1 Output

Some times you need to export results to a file. Typically this happens when you want to export a data table to a text file. R provides the functions write.table() and write.csv() for these purposes. These functions let you send a matrix or a data frame to a text file that will have a tabular format (i.e. rows and columns).

5.1.1 Concatenating output

You can use cat() to concatenate and print information to a file.

To show you how to use cat() let’s illustrate a simple example using the data frame mtcars that comes in R.

The goal is to generate a file mpg-statistics.txt with the following contents:

Miles per Gallon Statistics

Minimum: 10.40
Maximum: 33.90
Median : 19.20
Mean   : 20.09 
Std Dev: 6.02

Here is one way to do it. First, let’s assign the statistics to different objects:

After creating the objects containing the summary statistics, the next step is to export them to the text file mpg-statistics.txt via cat(). Assuming that the output file is in your working directory, here’s how you can send the set of strings to the text file:

The first line exported to mpg-statistics.txt is a string with the title "Miles per Gallon Statistics\n\n". Observe that we are using two new line characters "\n\n" to add some space between the title and the statistics. The rest of calls to cat() use the argument append = TRUE to concatenate the specified strings to the end of the text file without overriding the existing lines.

If you run the code of this example and look at the contents of mpg-statistics.txt, you will see the following output:

Miles per Gallon Statistics

Minimum: 10.4 
Maximum: 33.9 
Median : 19.2 
Mean   : 20.09062 
Std Dev: 6.026948 

As you can tell, the displayed values have a different number of decimal digits. If you just want to keep two decimal digits, you can use sprintf() and choose the format "%0.2f". Let’s re-export the lines:

Now the content of mpg-statistics.txt should look like this:

Miles per Gallon Statistics

Minimum: 10.40 
Maximum: 33.90 
Median : 19.20 
Mean   : 20.09 
Std Dev: 6.03 

Here is an exercise for you: How would you avoid writing that many calls to cat()?

5.1.2 Sinking output

Another interesting function is sink(). This function is very useful when you want to export R output as is displayed in the R console. For example, consider the output from summary()

You could assign the output of summary(mtcars) to an object and then try writeLines() to export the results to a file mtcars-summary.txt, but you won’t keep the same format of R:

To be able to keep the same output display of R, you must use sink(). This function will divert R output to the specified file:

Your turn: Use sink() to send the output from running a linear regression of mpg on disp with the function lm(). Also export the results from using summary() on the regression object. And/or try running a t-test between mpg and disp with t.test().

5.2 Exporting tables

Another interesting tool to export tables in LaTeX or HTML formats is provided by the R package "xtable" and its main function xtable().

The object reg_table is an object of class "xtable". What you do with this type of objects is print() them to a file.

To print reg_table in latex format to a .tex file:

To print reg_table in html format to an .html file: