So you want to do a clear all in r. The rstudio console allows you to manually clear cache variables if you click the little broom icon shortcut above the global environment. You can also do a clear console if you click the same shortcut icon above the r console. You can, however, clear the global environment by running command line code within the r workspace.
Two versions of the basic function
The command prompt function that clears a variable from the global environment comes in two forms. One is remove(objects, list) and the other is rm(objects, list). Each form works the same, the second is just quicker to write. The objects argument is a straightforward list of the names of the objects to be deleted- it allows you to delete multiple files from the file explorer or rstudio console. These names can be placed in quotes but they do not have to be. The list argument is a character vector containing the names of the objects targeted for removal. By using the ls() function you automatically produced a vector containing the names of all of the objects in the global environment, creating a clear all in r.
Partial clearing with rm()
You can use the rm() function to remove one or more variables. The most straightforward way is using the objects argument as shown below.
# clear all in r tutorial > a = 5 > b = c(1, 2, 3) > d = c(x=5, y=4, z=10, t=2) > > a  5 > b  1 2 3 > d x y z t 5 4 10 2 > > rm(a, b) > > a Error: object 'a' not found > b Error: object 'b' not found > d x y z t 5 4 10 2
The second way is to use the list argument. Because it is a character vector, it can be equated to an externally defined vector giving it greater flexibility. This allows different variables to be deleted when using a loop or any situation where the same section of code is reused.
# clear all in r tutorial with list argument > a = 5 > b = c(1, 2, 3) > d = c(x=5, y=4, z=10, t=2) > > a  5 > b  1 2 3 > d x y z t 5 4 10 2 > > rm(list = c("a", "b")) > > a Error: object 'a' not found > b Error: object 'b' not found > d x y z t 5 4 10 2
In both cases, each variable targeted for removal produces an object not found error when called the second time showing that it has indeed been removed.
Clear all with rm()
In this example, the rm() command prompt is called using the list argument where the argument is equated to the ls() function which returns a vector of the names of the objects in the global environment or browser cache.
# clear all in r with rm() > data(Titanic) > a = 5 > b = c(1, 2, 3) > d = c(x=5, y=4, z=10, t=2) > > a  5 > b  1 2 3 > d x y z t 5 4 10 2 > > rm(list = ls()) > > a Error: object 'a' not found > b Error: object 'b' not found > d Error: object 'd' not found
When this is done all the objects in the global environment are deleted including the data from loaded packages or any other type of file folder. This is a very effective and simple way to do a delete file or clear all in r to free memory or disk space on your device.
Now in all the above examples, rm() can be substituted with remove() and it will work just fine. They are just variations of the same function. This is a helpful tool for automatically clearing out of the console any variables or loaded packages that you are done using. It is also an excellent tool for clearing all the variables at the same time. It is a simple tool to use on any device, operating system, or web browser window, and one that can save you the time of manually clearing out the global environment.