R Functions for Getting Objects

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


Welcome, fellow programmers, to this exciting journey into the world of R functions! Today, we’ll explore four powerful functions: get(), get0(), dynGet(), and mget(). These functions may sound mysterious, but fear not; we’ll demystify them together and see how they can be incredibly handy tools in your R toolkit. So, let’s dive in!

The get() Function

The get() function is a versatile and often overlooked gem in R. Its primary purpose is to retrieve the value of a variable stored in an environment by specifying its name as a character string. The syntax of the get() function is straightforward:

get(x, pos = parent.frame())

Example get()

Let’s say you have a variable named my_variable stored somewhere in your R environment, and you want to access its value using the get() function:

# Sample variable in the environment
my_variable <- 42

# Using get() to retrieve the value
result <- get("my_variable")
[1] 42

Explanation of get()

In the example above, we used get("my_variable") to access the value of the variable my_variable. The function returned the value 42, which was stored in the variable.

The get0() Function

The get0() function is closely related to get(), but it has a subtle difference. It retrieves the unevaluated variable itself, rather than the value of the variable. The syntax of get0() is:

get0(x, pos = parent.frame())

Example get0()

Let’s use the same variable my_variable as before and see the difference between get() and get0():

# Sample variable in the environment
my_variable <- 42

# Using get0() to retrieve the variable itself
result <- get0("my_variable")
[1] 42

Explanation of get0()

In this example, get0("my_variable") returned the unevaluated symbol my_variable itself, not its value (42). This can be useful in certain scenarios, such as when you want to work with the symbol rather than its value.

The dynGet() Function

The dynGet() function is similar to get(), but it searches for the variable in a specified environment. The syntax is:

dynGet(x, ifnotfound = , minframe = 1L, inherits = FALSE)

Example dynGet()

Consider a scenario where you have a variable named num inside a custom environment, and you want to access it using dynGet():

# Create a new environment
custom_env <- new.env()

# Assign a variable inside the custom environment
custom_env$num <- 99

# Using dynGet() to retrieve the value
result_env <- dynGet("num", custom_env)
<environment: 0x0000026c4b6cf388>
result_num <- dynGet("num", custom_env$num)
[1] 99

Explanation of dynGet()

In this example, we used dynGet("num", custom_env$num) to access the value of the variable num from the specified custom_env environment. The function successfully retrieved the value 99.

The mget() Function

The mget() function is a workhorse when you want to retrieve multiple variables at once. It takes a vector of variable names as input and returns a named list with the values. The syntax is:

mget(..., envir = as.environment(-1))

Example of mget()

Let’s say we have two variables, x and y, and we want to retrieve their values using mget():

# Sample variables in the environment
x <- 10
y <- 20

# Using mget() to retrieve the values of multiple variables
result <- mget(c("x", "y"))
result # Output: a named list with values: $x [1] 10, $y [1] 20
[1] 10

[1] 20

Explanation of mget()

In this example, we provided the vector c("x", "y") to mget(), and it returned a named list with the values of both variables x and y.


Congratulations on reaching the end of this blog post! We’ve covered four essential functions in R: get(), get0(), dynGet(), and mget(). These functions enable you to access variables and their values efficiently, whether they reside in the global environment or custom environments. We hope you found this information useful and encourage you to try them out in your R projects. Happy coding, and may your R programming journey be filled with success and joy!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)