5 Saving Output Automatically

When working in the RStudio, R echoes commands, prints output, and returns error messages all in one place: the Console. However, the Console only buffers a limited amount of output (1000 lines by default), making it difficult to work with large quantities of output.

In most cases, you should be intentional about how you save output, such as saving datasets as RDS files and regression results as formatted tables with the stargazer package. Some cases call for saving all of our output, such as creating log files.

There are numerous methods of capturing printed output automatically. Here we will consider two basic methods that you might use when working within RStudio.

Each of these methods will save and read files from our working directory. When we use sink(), we will find the output file (“outputfile.txt”) in our working directory. When we use batch processing, we need to ensure our script (“testscript.R”) is saved in our working directory so that R can find it; we will then find the output file (“testscript.Rout”) in our working directory as well. Refer to the earlier discussion of the working directory for help finding and setting the working directory.

5.1 The sink() Function

One option is to use the sink() function. This redirects your output to a file, while commands and error messages continue to go to the console. This gives you clean output (SAS-style), and might be suitable for producing a simple report.

If you are running many similar commands with similar output, using this approach to create a single file quickly becomes difficult to read.

Use one sink() command with a filename to begin saving output, and another empty sink() command to stop.

sink("outputfile.txt")

t.test(rnorm(15, mean=2))

sink()

Which saves the following text in a file called “outputfile.txt”:


    One Sample t-test

data:  rnorm(15, mean = 2)
t = 9.4023, df = 14, p-value = 1.993e-07
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 1.855096 2.951549
sample estimates:
mean of x 
 2.403322 

It is also possible to sink() error messages by adding the argument type = "message". However, this quickly gets complicated, and can be difficult to interpret.

5.2 R BATCH Output

If you want to save a large quantity of output that includes the commands that produced it, you really want BATCH output (Stata-style output).

You must first save your script. Then you process that file.

For example, save a file (our example is called testscript.R) with the following commands in your working directory:

# testscript.R, used as an example

t.test(mpg ~ am, data=mtcars, var.equal=TRUE)

Then issue this command in the Console:

tools::Rcmd("BATCH --no-save testscript.R")

In the Files pane you can find the output file and open it:


R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> # testscript.R, used as an example
> 
> t.test(mpg ~ am, data=mtcars, var.equal=TRUE)

    Two Sample t-test

data:  mpg by am
t = -4.1061, df = 30, p-value = 0.000285
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -10.84837  -3.64151
sample estimates:
mean in group 0 mean in group 1 
       17.14737        24.39231 

> 
> proc.time()
   user  system elapsed 
   0.28    0.17    0.56 

The R CMD BATCH command has a lot of options you could specify, mostly manipulating how your R session is configured. For instance, the --no-save option tells R not to save your workspace at the end of this script.

5.3 Exercises

  1. sink() the results of these lines of code:
x <- rnorm(20, mean = 3, sd = 3)
t.test(x)
  1. Copy the code below to a script and batch process it.
data(mtcars)
summary(mtcars)
t.test(mpg ~ am, data=mtcars)