R session definition

What is the difference between an R session, workspace, and image?

I feel these terms do not have a single definition, and can have slightly different meanings in different contexts.

I would say the R workspace is what R keeps in memory while you're working: the objects and functions you created, the libraries you loaded. An image would be the file you get when you use save(), that contains the content of the workspace. The R session is what you have in front of you when you start R, and disappears when you close R.

So when you start R, you are in a session, you make computations that fill the workspace. Then, if you use save("path/to/file.rda"), you write your workspace into an image. You can close R, loosing your session and workspace. Then you re-open R and run load("path/to/file.rda"), and you get your old workspace in a new session, by loading that image.

Note that by default, R offers to automatically save an image of your current workspace when you try to leave your session (and automatically loads it when you open a new session from the same directory).

Also note that it's often seen as bad practice to save your whole workspace, a better approach is to only save your objects of interest using saveRDS(), that way when you close the session, the workspace is definitely and irretrievably destroyed, and when you open a new session you have a fresh workspace, that is not polluted by anything you did last time (and you can use readRDS() to selectively retrieve the objects you actually need).

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.