nanxstats / r-base-shortcuts

⚡ Base R shortcuts: A collection of lesser-known but powerful idioms and coding patterns for writing concise and fast R code
https://nanx.me/blog/post/r-base-shortcuts/
158 stars 16 forks source link

R-Insight: Save All Data Objects in a Workspace to a Single File #6

Closed brichard1638 closed 1 year ago

brichard1638 commented 1 year ago

When working in the R environment, data is created, collected, and used in the form of various list objects, vectors, models, and datasets. By applying the save.image function found in Base R, the entire active workspace can be saved to a single data file. The R file extension used must be .RData. In RStudio, data objects are extracted and subsequently converted into an .RData file from the Environment tab.

save.image(file = "my_work_space.RData")

To load the entire workspace back into an RStudio active session, apply the following code: load("my_work_space.RData")

NOTE: If no formal file path is provided in the file argument, the file will be saved in the user's Documents folder.

nanxstats commented 1 year ago

From the software engineering best practice perspective, I think it's generally recommended to use functions like save(), saveRDS() for saving (serializing) individual objects, or explicitly save the data needed in a suitable file format, instead of using save.image() to save everything. Specific reasons might include: