Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving workspace image, in R

Tags:

r

workspace

When closing R Studio at the end of a R session, I am asked via a dialog box: "Save workspace image to [working directory] ?"

What does that mean? If I choose to save the workspace image, where is it saved? I always choose not to save the workspace image, are there any disadvantages to save it?

I looked at stackoverflow but did not find posts explaining what does the question mean? I only find a question about how to disable the prompt (with no simple answers...): How to disable "Save workspace image?" prompt in R?

like image 923
Rtist Avatar asked Aug 21 '19 09:08

Rtist


People also ask

What does saving workspace image mean in R?

The workspace is your current R working environment and includes any user-defined objects (vectors, matrices, data frames, lists, functions). At the end of an R session, the user can save an image of the current workspace that is automatically reloaded the next time R is started.

How do I save workspace in R?

You can also save workspace using the file menu. For this, click the File menu and then click save workspace. You will see the dialog box, browse to the folder where you want to save the file and provide the file name of your own choice.

How do you save an image in R?

If you're running R through Rstudio, then the easiest way to save your image is to click on the “Export” button in the Plot panel (i.e., the area in Rstudio where all the plots have been appearing). When you do that you'll see a menu that contains the options “Save Plot as PDF” and “Save Plot as Image”.

Should I save workspace images?

Saving your workspace creates an image of your current variables and functions, and saves them to a file called ”. RData”. When you re-open R from that working directory, the workspace will be loaded, and all these things will be available to you again. But you don't want that, so don't save your workspace.


2 Answers

What does that mean?

It means that R saves a list of objects in your global environment (i.e. where your normal work happens) into a file. When R next loads, this list is by default restored (at least partially — there are cases where it won’t work).

A consequence is that restarting R does not give you a clean slate. Instead, your workspace is cluttered with existing stuff, which is generally not what you want. People then resort to all kinds of hacks to try to clean their workspace. But none of these hacks are reliable, and none are necessary if you simply don’t save/restore your workspace.

If I choose to save the workspace image, where is it saved?

R creates a (hidden) file called .RData in your current working directory.

I always choose not to save the workspace image, are there any disadvantages to save it?

The advantage is that, under some circumstances, you avoid recomputing results when you continue your work later. However, there are other, better ways of achieving this. On the flip side, starting R without a clean slate has many disadvantages: Any new analysis you now start won’t be in a clean room, and it won’t be reproducible when executed again.

So you are doing the right thing by not saving the workspace! It’s one of the rules of creating reproducible R code. For more information, I recommend Jenny Bryan’s article on using R with a Project-oriented workflow

But having to manually reject saving the workspace every time is annoying and error-prone. You can disable the dialog box in the RStudio options.

like image 170
Konrad Rudolph Avatar answered Oct 13 '22 14:10

Konrad Rudolph


The workspace will include any of your saved objects e.g. dataframes, matrices, functions etc.

Saving it into your working directory will allow you to load this back in next time you open up RStudio so you can continue exactly where you left off. No real disadvantage if you can recreate everything from your script next time and if your script doesn't take a long time to run.

like image 29
Bemused Avatar answered Oct 13 '22 14:10

Bemused