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. With the script editor as the active window, 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.
When you shut down RStudio, any open scripts are automatically stored by default, and reappear when you start up RStudio again. However, they are not available outside of RStudio until you save them.
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 computer time to run.
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.
x <- data.frame(value = rnorm(100))
write.csv(x, "mydata.csv", row.names = F)
We read CSV files in by assigning them as an object:
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 the RDS format can handle many other types of objects, such as fitted models or results from statistical tests.
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 (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.
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
You save plots pretty much as you expect. In the Plots pane, click on Export - Save as Image … on the toolbar. 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. The idea is that you are usually better off saving data objects. You also have the option of automatically saving Console output.
Keep in mind, too, that the Console is limited to the last 1000 lines of text you output. If you have been working for a long period of time, or if you accidentally print a lot of data, your first results disappear and can only be recovered by rerunning your commands.