Viewing previous graphs in R for Windows

HOME You have produced several plots in R, and now you want to go back and take a look at one of those old plots. On a Mac or RStudio, you can do that with the navigation keys. R for Windows has the same functionality but it is not enabled by default (for good reasons, see Warning below). Here's how to enable it.

Short term fix

To start recording plots, go to the History menu in the graphics window and check Recording. The current plot and plots you do subsequently will be added to the record. You can scroll through recorded plots with the PgUp and PgDn arrow keys.

Instead of turning Recording on, you can add individual plots to the record with the History > ADD menu option or with the Insert key.

The plots are recorded in an object called .SavedPlots in your workspace, so they do not disappear when you close the graphics window: you can open a new graphics window and still review the old plots. However, plots in the new graphics window will not be recorded unless you turn History > Recording on again.

You can open a graphics window with recording already on using the command windows(record=TRUE) before the first plot command.

All of those options require forethought: you need to know that you will want to return to that plot before you obliterate it with the next one. That rarely works for me! I like to change the default behaviour so that recording is on when I do the first plot.

Changing the default setting

You can change the default settings of the graphics window with the windows.options function: do windows.options(record=TRUE) and any graphics windows opened will have recording turned on. That might be useful in a specific script, but I don't want to have to do that every time I open R. Instead, I put the command into my .Rprofile file. The .Rprofile file (note the dot at the start of the file name) is an optional file of R code which is run every time that R starts up. Here's how to put it into your .Rprofile file:

  1. Check the location with Sys.getenv("R_USER"); it is usually your Documents folder.
     
  2. Check to see if you already have an .Rprofile file there. Open Documents in File Manager, go to the View tab and make sure "File name extensions" is checked (otherwise file names beginning with a dot will not be shown).
     
  3. If it exists, open with Notepad or other text editor and add the code below at the end of the file.
     
  4. If it does not exist, open Notepad with a new file, and save it in Documents with the name ".Rprofile" including the quotation marks, as shown in the screen shot below. This prevents Notepad adding ".txt" to the end of the name.

  1. Copy-paste the following code into your .Rprofile file:
setHook(packageEvent('grDevices', 'onLoad'),
   function(...)
       grDevices::windows.options(record=TRUE) )
  1. Save and close.

When you open R in future, it will read your .Rprofile file and graphics windows will open with recording enabled.

Warning!

The plots are recorded in your workspace in an object called .SavedPlots. This can become very big, and can take up a large proportion of the limited memory available to R. This can happen if:

  • you have very large plots with thousands of points; or
  • you  have lots of plots, even if each one is small.

If you routinely save your workspace when you exit R, then restore it when you start R again, the .SavedPlots object will continue to grow with every session.

You can check the size with object.size(.SavedPlots) and remove it either with the History > Clear history option in the graphics window or with rm(.SavedPlots). Just turning off History > Recording will not remove the past records.

Don't rely on the plot history as a permanent record of your plots. If you need that, save them as JPG or PDF files with the File > Save as... menu options.

Updated 17 Oct 2016 by Mike Meredith