R projects
You should probably read Using Projects - RStudio Support. R Projects are super useful, but they are not meant for saving data from your R environment. They are exclusively used by the RStudio code editor. One of the nicest things they do is automatically set your working directory to the project directory when you open one. They also remember what files you had open in RStudio, and other editing-related preferences and such. Definitely use RProjects!
R data
.RData
is a file of R objects. You can create an R data file from within R (not just RStudio) using the save()
command and later load them back into your workspace with load()
. You can save all the objects in your workspace (save.image
does this automatically - it's a wrapper around save()
) or only specific objects. See ?save
for details. (For single objects, .rds files created with saveRDS
are preferred.)
For many years (since long before RStudio came to be) the default RGui has given the option to save all the objects in your workspace to an .RData file on exit. RStudio also gives this option (unless you turn it off).
The diskette "save" icon at the top of your the editor pane in RStudio does not save R objects, it saves only the code you have written in your scripts. The "Environment" tab also has a diskette save icon, which will save R objects.
Best practices
This gets into opinions of style; there is no definitive answer. My personal preference is to never do blanket save of all objects in my workspace because it enables a bad habit of not keeping the code needed to create those objects. I save all my scripts, and if a particular object(s) takes a long time to create, I will script the saving of it -
saveRDS(object = final_model, file = "final_model.rds")
I treat a model or a cleaned data set much like a nice plot in code - keep the code to make it in case you want to tweak it, but save the output to a file so you don't have to run the code to recreate it every time you want to look at it.
For larger projects I try to keep the scope of an individual script small and I often number scripts (in the order I'd want to run them to start from the beginning) as suggested by answers to Workflow for statistical analysis and report writing. Most scripts begin by reading in objects they depend on and end by saving their outputs.