Print All Data Objects in Workspace in R (2 Examples)
In this R tutorial you’ll learn how to return all data objects in the RStudio workspace.
Table of contents:
Let’s dig in:
Construction of Example Data
The following data will be used as basement for this R programming tutorial:
data <- data.frame(x = 1:5, # Create example data y = 9:5) vec <- 1:10 # Create example vector
Let’s print these data!
Example 1: Print Names of All Data Objects in Workspace Using ls() Function
The following R programming code demonstrates how to get the names of all objects in the R programming workspace (or environment).
For this task, we can use the ls function as shown below:
ls() # Print names of data objects # [1] "data" "vec"
The previous output of the RStudio console shows the names of all data objects that are currently stored in our workspace.
Example 2: Print Content of All Data Objects in Workspace Using ls() & mget() Functions
In Example 2, I’ll show how to return the content of all data objects in a list.
For this, we can use the mget function and the ls function as shown in the following R syntax:
mget(ls()) # Print content of data objects # $data # x y # 1 1 9 # 2 2 8 # 3 3 7 # 4 4 6 # 5 5 5 # # $vec # [1] 1 2 3 4 5 6 7 8 9 10
As you can see, our data objects data and vec contain a data frame and a numeric vector.
Video, Further Resources & Summary
I have recently released a video on my YouTube channel, which demonstrates the R programming codes of this tutorial. You can find the video below:
Furthermore, you may read some other articles on my website. I have released numerous posts on topics such as factors, missing data, and vectors:
- Clear Data Object from Workspace in R
- Save & Load RData Workspace Files in R
- What’s the Difference between rm() & rm(list=ls())?
- Remove All Objects But One from Workspace
- The R Programming Language
In summary: In this tutorial, I have shown how to print all data objects in the global environment in R programming. Please let me know in the comments, in case you have any further comments and/or questions. Furthermore, don’t forget to subscribe to my email newsletter to receive regular updates on the newest tutorials.