2 Saving R 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, making it difficult to work with large quantities of output.
There are numerous methods of capturing printed output automatically. Here we will consider two basic methods that you might use when working within RStudio.
Before that, it is important to become familiar with the concept of a working directory.
A working directory, simply put, is the directory or folder where you are working.
If you tell R to save or load a file without specifying an absolute file path, it will
start from your working directory. When you use the
sink() and BATCH commands below to
save your output, it will save them in your working directory by default.
Finding your working directory is simple. It is in the bar at the top of the Console pane in
RStudio, or you can print it with the
To change your working directory, either use the menu at Session - Set Working Directory
or use the
setwd() function with a relative or absolute file path in quotes.
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 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.
sink command to begin saving output, and another
empty (NULL) 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. However,
this quickly gets complicated, and can be difficult to interpret.
If you want to save a large quantity of output that includes the
commands that produced it, you really want
You must first save your script. Then you process that file.
For example, save a file, testscript.R, with the following commands:
# 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
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.
2.3 Exercises - Saving Output
- Use the scripts you created in section 1.3.5, Exercises - Saving Files.
- Sink the results of the one-sample t-test.
- Batch process the two-sample t-test script.