15

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?

Rtist
  • 3,825
  • 2
  • 31
  • 40
  • 1
    The workspace image, is a copy of your current Environment. Which includes anything that is user defined, from data frames to functions. If you are new to R, I would recommend https://www.statmethods.net/interface/workspace.html – David Weber Aug 21 '19 at 09:26
  • My answer at https://stackoverflow.com/a/67016157/5114585 along with my LI posts links including https://www.linkedin.com/feed/update/urn:li:activity:6786156716447830016?utm_source=linkedin_share&utm_medium=member_desktop_web will answer all your queries related to saving/not saving workspace image. – Dr Nisha Arora Aug 08 '22 at 03:34

3 Answers3

18

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

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.

Bemused
  • 21
  • 1
  • 1
    “No real disadvantage” — No, there are very tangible disadvantages. In fact, the prevailing advice is to *never* save the workspace since it’s (a) unnecessary, and (b) leads to irreproducible state and hard to diagnose bugs. – Konrad Rudolph Aug 21 '19 at 10:40
-1

The only thing I have to add here is that you should consider seriously that some people may be working on ongoing projects, i.e. things that aren't accomplished in one day and thus must save their workspace image so as to not start from the beginning again.

I think, best practice is: its ok to save your workspace, but your code only really works if you can clear your entire workspace and then rerun it completely with no errors!

Gmichael
  • 526
  • 1
  • 5
  • 16
  • Saving the workspace image is fundamentally the wrong approach, and is pretty bad advice in all situations I can think of. Firstly, you absolutely don’t need to shut down your project at the end of the day — leave it running! Secondly, “start from the beginning” isn’t a problem at all if you’ve saved the code (which you should obviously *always* do), unless said code takes a very long time to run. If that’s the case, intermediate results should always be saved and restored *explicitly*, not via RStudio options, to ensure the code is reproducible. – Konrad Rudolph Nov 30 '21 at 16:11
  • In sum, it’s definitely wrong to claim that you ever “must” save your workspace: it’s never necessary. And, more than that, it’s never better than solving the issue differently: it’s never appropriate, both in my experience and in that of most if not all other R experts (and enabling the option never outweighs the substantial problems that this option creates; even the RStudio authors agree with this, and the only reason this ill-conceived option still exists is backwards compatibility). – Konrad Rudolph Nov 30 '21 at 16:13