Skip to content


How to prevent R from restoring saved workspace

This post in a nutshell: start R, type getwd(), go to the folder returned, and remove .RData.

I took me a while to understand what saving a workspace meant (in case you’re wondering too, it saves the current state of your R session into a file so when you open R again it will be as if you didn’t leave it), but then I found myself dealing with another problem: no way to prevent R from loading the last saved workspace, even if I chose not to save my workspace the last time I close R. So, any time I’d start R I’d get.

[Previously saved workspace restored]

That’s not such a huge problem since I can just clear my workspace (rm(list = ls(all = TRUE));) and then save that empty workspace, but still it felt like start up was slightly slower.
A first simple fix is to start R with the command-line option --no-restore. But that’s not that satisfying, as it’s either too permanent (e.g. if I add this to R’s shortcut, whenever I’ll really want to load a workspace I’ll need to change the shortcut again) or not permanent enough (if I don’t edit R’s shortcut, every time I start R I’ll need to type extra stuff).
So, the proper fix is to manually delete the worksheet saved in R’s start-up folder. To find that folder, simply type getwd() right after starting R. Then go to this directory and remove the file named .RData. And voilà, you’re done already. The next time you start R, it won’t load a workspace… unless you saved one again, of course.

Note that on Linux files with a name starting with a dot are hidden, use ls -a to see them.

Sources:

Update (2012-06-19): how to “unload” (kind of) the workspace

I noticed in the visit stats that some people were coming here after searching for “unloading workspace” and similar queries. I don’t think there is such a thing as “unloading” a workspace, but there’s a simple command to empty the workspace: rm(list = ls()). Basically, all this does is:
1. list all workspace objects (that’s what ls() does), and
2. remove the objects listed in 1. (that’s what rm() does).
Then, if you exit right after that and choose to save the workspace, next time you run R the loaded workspace will of course be empty. But it will still be loaded (message saying “restoring previously save workspace”).

Posted in R (R-project).


4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Alex R.E. says

    Thanks.

  2. user@123 says

    Thanks, this is very useful, I was wondering how to do that

  3. Langdon says

    The top post is perfect, thank you.

  4. Vincent says

    Under Tools you have Global Options where you can (de)select R to restore .Rdata into workspace at startup



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA