3.1 Saving variable data

If you started a new Project for your work, at the end of the day you can select File > Close Project. This will save all of your variables, your history, your open files and a range of other information. When you then re-open R, you can double click on the project file (which will have the suffix .Rproj) to continue where you left off.

As well as saving your entire project, you can save just specified variables, using the save() command. The save function saves the data as a binary object and the file prefix generally used for R binary objects is “.Rdata”.

In the following worked example, we will save the mandm variable that was created in the previous chapter. We then remove this variable from our workspace using the rm() function and check that it has been removed using the exists() function.

colours <- c("red", "yellow", "green", "blue", "orange")
mandm   <- sample(colours, 1000000, replace=TRUE)
save(mandm,file="chocolate.Rdata")

# Delete 'mandm' from the workspace
rm(mandm)

# Comfirm it is deleted
exists('mandm')
## [1] FALSE