I'm trying to figure out how saving works in R Studio.
When i create a new project, a .RProj file is created. Whenever I work in R Studio, Save and Save As are greyed out in the File menu. The only way I know how to create a .RProj file is when starting a new project.
In the environment section, I can see a floppy disk Save icon. When I click that, it creates a .RData file. When ever I want to save, I click on that save icon and overwrite the file.
Can someone please explain what the best practices are for saving when using R Studio and the key distinctions between the .RProj and .RData files?
Now, you can take advantage of RStudio automatically saving and remembering anything typed in a script file and just close RStudio without saving this script to a named file. However, if anything happened to your computer you run the risk of losing this information. So I strongly recommend having a single script file for an assignment or project. To save the script, you can either click on the blue save icon, use the keyboard commands Ctrl + S on Windows or Command + S on Mac OS or go to File > Save. This file will be open in RStudio the next time you reopen RStudio unless you click on the X on the file tab to close it.
Many other types of files can be opened in RStudio besides RScripts. All of these files or data views will be created as tabs in this upper left hand pane. I will discuss importing data into RStudio in my next blog post, once data is imported, if you view the file it will be available as a tab.
Here is what it will look like to view data in a tab (with the previous script saved as “frisbeeDistAnalysis.R”:
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!
.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.
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.