Saving R Output to a File

Doug Hemken

June 2020

R notes | R User Interface

Saving a large amount of R output

When working in the RStudio or R Console, R echos commands, prints output, and returns error messages all in one stream. However, the Console only buffers a limited amount of output, making it difficult to capture large quantities of output.

The sink() and capture.output() functions

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, SAS-style output, and might be suitable for producing a report. capture.output() is similar.

It is also possible to sink() error messages by specifying type=“message”. To sink both output and messages to the same file requires two separate calls to sink(). Then the difference between this and your Console is that the file does not contain an echo of your commands.

R BATCH output

However, if you want to save a large quantity of output that includes the commands that produced it, as well as any warnings or error messages, you really want BATCH output (similar to Stata output).

If you are familiar with the Windows Command window, this is fairly easy to do. Open the Command window, navigate to the directory where you want to work (usually where your *.r file is located), and issue a command like

> R CMD BATCH filename.r

On Windows computers you will have to include a path in front of “R”:

> "C:\Program Files\R\R-4.0.1\bin\x64\R" CMD BATCH --no-save filename.r

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

Running BATCH via drag-and-drop

On a Windows computer it is pretty easy to create a little desktop program that you can use to produce BATCH output. It involves turning the command line command (above) into a Windows batch program. Create a little text file named "R.bat" and save it on your desktop. In this file you can put one line of code:

"C:\Program Files\R\R-4.0.1\bin\x64\R" CMD BATCH --no-save %1 %1.Rout

Note that this will be used by Windows, not R, so it is important the path to R use backslashes instead of forward slashes!

(Adjust the path for your version of R.)

When you drag-and-drop an R script onto this batch program, R executes the script and saves the output in a file with the same name and a .Rout file extension.

Submitting BATCH jobs from the Console

It is actually pretty easy to produce BATCH output from the console, however the commands used are pretty obscure. The basic idea is that you have your current R session pass a call to the operating system, running a second, batch session that produces output in the format you need.

To begin, you need to have a *.r file (a script) saved and ready to go.
I have a little file, named “test.r”, that has the lines


For this example, I saved that file to my network drive, “U:\”. Then in the R console, issue a command like

system("R CMD BATCH U:/test.r") 

If you work with R on Linux, or work directly with R in BATCH mode, that should look familiar.

This command works if (1) R is on the operating system’s PATH (which is not the Windows default), and (2) there are no spaces in the file path/name.

Note that you cannot use single backslashes, ’', in the file name, because R manipulates the character string before passing it to the OS. And “CMD BATCH”, however, must be capitalized, as this is being processed by R!

Since the previous version will not work for most people without manipulating their PATH environment variable in Windows, a more robust version is:

system2("C:/Program Files/R/R-4.0.1/bin/x64/R", args="CMD BATCH U:/test.r")

The previous command works regardless of the PATH variable in Windows. However, as you can see by looking at the path to R, this code will break if you try it with a different version of R. So an even more robust version is:

system2(paste(R.home("bin"), "/R", sep=""), args="CMD BATCH u:/test.r")

Finally, what if our project folder has spaces in its name?

system2(paste(R.home("bin"), "/R", sep=""),
    args=paste("CMD BATCH", shQuote("u:/test folder/test.r")))

is one way to do this if your file name/path has spaces.

A BATCH function

This can be simplified by wrapping it in a function as follows:

batch <- function(x){
        stopifnot(is.character(x) & length(x)==1)
        system2(paste(R.home("bin"), "/R", sep=""),
            args=paste("CMD BATCH", shQuote(x)))


Which produces this file:

cat(paste(readLines("test.r.Rout"), collapse = "\n"))

R version 4.0.1 (2020-06-06) -- "See Things Now"
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.

[Previously saved workspace restored]

> date()
[1] "Fri Jun 19 10:51:22 2020"
> print(1:10)
 [1]  1  2  3  4  5  6  7  8  9 10
> proc.time()
   user  system elapsed 
   0.29    0.12    0.50