4  Saving Commands and Results

Like most statistical software, you save the pieces of an R project in several different files.

4.1 Scripts

Scripts and your data are the most important pieces of any project. With good scripts and your original data, your work is documented and reproducible.

You save scripts pretty much the way you would expect. Click File - Save As and give your file a name, along with the file extension “.R”. Although this is just a plain text file, the “.R” file extension will ensure that RStudio and the operating system recognize this as an R script, enabling features such as syntax highlighting and double-clicking .R files to open RStudio.

You can open previously saved scripts from the Files pane (lower right, tabbed with Plots). Just click on the file name.

4.2 Data Objects

Having read your data into R, cleaned it, and created any other data objects (including results objects such as fitted models), you may want to save your intermediate data. This is especially useful if data wrangling or modeling takes a lot of time to run.

Any data objects in your environment only exist within RStudio. They are not saved anywhere on your computer If you close RStudio, they are deleted and gone forever. If you want to use any of these files in a later R session, you need to save them to your computer’s file system.

Datasets can be saved as CSV files with write.csv() if we give the function our dataframe name and a file name to save it as. We should also specify row.names = FALSE so that the row names are not saved as an additional column in the data. In most datasets, the “names” are just the row numbers.

x <- data.frame(value = rnorm(100))
write.csv(x, "mydata.csv", row.names = FALSE)

We read CSV files in by assigning them as an object. More accurately, assigning them to an object creates a data object. Simply running read.csv("mydata.csv") would create text output. That would allow us to see the values in the dataframe, but not use it in a later step.

y <- read.csv("mydata.csv")

A more flexible alternative to the CSV file is a RDS file. RDS files have two main advantages over CSV files:

  • They take up less disk space, which is good if we work with large datasets. This also means less time saving and loading files.
  • CSVs can only save rectangular data, such as dataframes, but we can save any kind of data object as an RDS file, including fitted statistical models.

The process for saving and loading RDS files is similar to that of CSV files. Use saveRDS() to save an object, and load it by using readRDS() and assigning it to an object.

x <- data.frame(value = rnorm(100))
saveRDS(x, "mydata.rds")
y <- readRDS("mydata.rds")

Where did these files save? These examples of saving data have just specified a file name, but not where on our computer we wanted to save them.

4.2.1 The Working Directory

These files were saved in our working directory. A working directory, simply put, is the directory (another term for a folder) where we are working. If we tell R to save or load a file without specifying an absolute file path, it will start from your working directory. If we just give a file name and no path at all, as we did above, it will be saved in our working directory.

Finding your working directory is simple. It is in the bar at the top of the Console pane in RStudio, or we can print it with the getwd() function.

getwd()
[1] "/Users/bbadger/"

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.

The default working directory can also be changed in Tools - Global Options, the first option in the General tab.

For more on working directories, refer to this section from Data Wrangling with R.

4.3 Plots

To save a plot, click on Export - Save as Image… in the toolbar of the Plots pane. Pick an appropriate image format. The options depend on your computer’s operating system. Which format you want depends on how you intend to use the saved image. Then give the file a name.

This can also be automated with scripted commands. See help(Devices).

You will not be able to reopen saved graphics within RStudio.

To learn more about plotting in R, be sure to read our materials on Data Visualization in R with ggplot2 and on Using R Plots in Documents, which covers saving plots in detail.

4.4 Text

RStudio does not make it simple for you to save the contents of the Console. Instead, save the data object that you used to print the result.

For example, you may have some output from a regression model that you would like to recall later. (This example runs very quickly, but the procedure extends to models that can take minutes or hours or days to run.)

Instead of trying to save the output from this model:

lm(mpg ~ am * wt, data = mtcars)

Call:
lm(formula = mpg ~ am * wt, data = mtcars)

Coefficients:
(Intercept)           am           wt        am:wt  
     31.416       14.878       -3.786       -5.298  

Assign that model to a data object, and then save the fitted model to your computer:

mod <- lm(mpg ~ am * wt, data = mtcars)
saveRDS(mod, "regression.rds")

There are also specialized packages you can install to save specific types of data objects. The exercise in Using Packages will introduce you to the stargazer package, which can save tables of regression model estimates as Microsoft Word documents.

4.5 Exercises

Copy the code below to a script.

x <- rnorm(1000)
hist(x)
  1. Save the script as a .R file.

  2. Save the object x as an RDS file.

  3. Save the plot as a PNG file.