If you're using RStudio, please consider never using the rm(list = ls())
approach!* Instead, you should build your workflow around frequently employing the Ctrl
+Shift
+F10
shortcut to restart your R session. This is the fastest way to both nuke the current set of user-defined variables AND to clear loaded packages, devices, etc. The reproducibility of your work will increase markedly by adopting this habit.
See this excellent thread on Rstudio community for (h/t @kierisi) for a more thorough discussion (the main gist is captured by what I've stated already).
I must admit my own first few years of R coding featured script after script starting with the rm
"trick" -- I'm writing this answer as advice to anyone else who may be starting out their R careers.
*of course there are legitimate uses for this -- much like attach
-- but beginning users will be much better served (IMO) crossing that bridge at a later date.