Clean up “everything” in RStudio

This is a tip for how to clean up your RStudio windows.

For workspace:

You can use rm() to clean all objects in current environment

rm(list=ls())

Or if you only want to remove specific object or only a group of new generated objects, try the following:

rm(list='obj_name')
obj.list <- ls()  #Save the names of the existing objects
....
rm(list=setdiff(ls(), obj.list))  #Remove any new generated objects

 

For console:

You can press Ctrl – L manually. Of course, it would be nice to do this programmatically. So try this:

cat("14")  # or cat("f")

 

For plot windows:

Try to use dev.off(), it will clost all existing graphical device and only keep Null device (device 1). If you have other graphical devices open (e.g. pdf or png) and don’t want them to be closed, you can use dev.list() to figure out which graphical device is RStudio’s.

dev.off(dev.list()["RStudioGD"]