Here is how to clear the RStudio console, environment, and plot windows (panes) individually or at the same time by using code (a combination of functions).
Clear the RStudio console, environment, plot panes and free up memory at once
rm(list = ls(envir = globalenv()), envir = globalenv()); if(!is.null(dev.list())) dev.off(); gc(); cat("\014")
Clear the RStudio console window
cat("\014")
Clear the RStudio environment window
rm(list = ls(envir = globalenv()), envir = globalenv())
Clear the RStudio plot window
if(!is.null(dev.list())) dev.off()
Free up memory for RStudio
There is a great function from the R base package called gc().
It is also possible to customize the RStudio startup and add it to your RProfile as an additional function in the base package. It looks like this.
utils::assignInNamespace( "clear.it", function(...) { rm(list = ls(envir = globalenv()), envir = globalenv()) if(!is.null(dev.list())) dev.off() gc() cat("\014") } , "base")
For more detailed instructions on how to do it in your RProfile.site file go here.
As a result, you can call that function and clear RStudio panes with code any time you need a little cleanup.
Remember to add an additional free line at the end of your Rprofile. Reason here.
Leave a Reply