Saving in R Studio

I am trying to figure out how work is saved in R Studio.

When I create a new project, a .RProj file is created. Whenever I work in R Studio, Save and Save As grayed in the File menu. The only way I know how to create a .RProj file is to start a new project.

In the "Environment" section, I see a floppy disk save icon. When I click on it, it creates a .RData file. When I want to save, I click on the save icon and overwrite the file.

Can someone explain what is best used for saving when using R Studio and the key differences between .RProj and .RData files?

+6
source share
2 answers

Projects R

You should probably read Using Projects - Support for RStudio . R Projects are very useful, but they are not designed to save data from your environment R. They are used exclusively 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 it. They also remember which files you opened in RStudio, as well as other settings related to editing. Definitely use RProjects!

R data

.RData is a R object file. You can create an R data file from R (not just RStudio) using the save() command, and then load them back into the workspace using load() . You can save all the objects in your workspace ( save.image does this automatically - this is a wrapper around save() ) or only certain objects. See ?save more details. (For individual objects, .rds files created with saveRDS .)

For many years (so long before the advent of RStudio), by default, RGui has provided the ability to save all objects in your workspace in a .RData file upon exit. RStudio also provides this option (unless you disable it).

The Save icon on a diskette at the top of the editor panel in RStudio does not save R objects; it only saves the code that you wrote in your scripts. The Environment tab also has a diskette save icon that saves R.

Recommendations

This concerns style looks; no final answer. My personal preference is to never do a hidden save of all objects in my workspace, because it allows me to have the bad habit of not storing the code needed to create these objects. I save all my scripts, and if a specific object takes a long time, I will script save it -

 saveRDS(object = final_model, file = "final_model.rds") 

I refer to the model or the cleared dataset as a good plot in the code - keep the code in case you want to configure it, but save the output to a file so that you do not have to run it to recreate it every time you want to look on him.

For large projects, I try to keep the scope of an individual script small, and I often use number scripts (in the order in which I would like to start them from the very beginning), as suggested by the answers to the Workflow for statistical analysis and reporting . Most scripts start by reading in the objects on which they depend, and ending with saving them.

+13
source

The save() function creates a representation of your R objects to the specified file. Later, objects can be read with the specified functions load() , attach() or data() in some cases, for example, for R created in data sets. It allows you to save objects and functions created in a .RData file. It is very important to include the .RData extension when specifying the file path. The help file will provide you with additional information.

RStudio projects allow you to divide your work into several contexts, each with its own working directory, workspace, history and source documents. The Create Project command allows you to create a project in a new or existing directory..RData is written to the project directory by default. This is a useful tool for managing workspace. You can find a full detailed description of the project capabilities https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects .

0
source

All Articles