Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

Tips for cleaning-up an R session

Sometimes one just needs a “clean slate” in R, particularly when using R Studio. Here are my quick tips for cleaning up an R session in R Studio:

Remove objects from the current workspace

To remove all objects, use the rm(list=ls()) command. If you only want to remove a single object, this can be specified as follows:

rm(NameOfObject)

Detach all packages

Sometimes there can be conflicts between packages (for instance between plyr and dplyr). To remove all running packages, use the following command:

invisible(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), detach, character.only=TRUE, unload=TRUE))

Fix problem with ggplot2 not displaying in R Studio

Sometimes ggplot2 plots just stop working in R Studio. This is a real nuisance. The most reliable way to remedy this that I have found is to use the following command:

dev.off()

Keep entering the command until R displays:

> dev.off()
Error in dev.off() : cannot shut down device 1 (the null device)

Now ggplot2 should work again.

   

Comments

No comments have yet been submitted. Be the first!

Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.