List and delete objects in R

Object listing and removal from your programming environment

During the conception of your R scripts, your global environment can contain a lot of objects. Let's find how to manage and clean up your programming environment.

List objects from global environment in R

To list the objects of the global environment in R, you have to use the ls() function. Without parameter, it lists all the objects of the current environment, generally the global environment. Here is an example:

ls()
[1] "myDataframe" "myDataframe2" "myList" "myVector"

The ls() function return a character vector that can be processed on our scripts. The ls() function can contain parameters to identify the environment to be analyzed thanks to the name attribute.

Remove object in R

You can remove objects in R with the rm() function. Here is an example:

rm(myObject)
rm(myDataframe, myList)

Note that to remove several objects from your programming environment, you're not forced to get it on a vector. You just have to add them as function parameters. At last, not that the rm() function doesn't return any object as response.