The get() function retrieves an object by name when you don’t know the object’s name until runtime. It’s the complement to assign();if you’re creating variables dynamically, you’ll need get() to retrieve them.

I use get() when I’m working with programmatically generated variable names or building functions that need meta-programming capabilities.

When to Use get()

You’ll use get() when:

  • Retrieving variables created with assign()
  • Working with dynamically generated variable names
  • Building functions that operate on named objects
  • Meta-programming and introspection
  • Conditional variable retrieval

Syntax Variations

get() - Retrieve single object:

get("object_name", envir = .GlobalEnv)

get0() - With custom fallback:

get0("object_name", ifnotfound = "default_value")

mget() - Retrieve multiple objects:

mget(c("object1", "object2", "object3"))

Examples with Explanations

Use get() Function to Get Named Object

Let’s see how to use get() function to get names object:

# Define vector
Reading <- c(12.39,9.25,14.15,13.48,81,78,82,83)

# Get named object
get("Reading")

Output:

[1] 12.39  9.25 14.15 13.48 81.00 78.00 82.00 83.00

The output shows values from vector means we successfully retrive vector.

Let’s see example which try to retrive object which not exists:

# Define vector
Reading <- c(12.39,9.25,14.15,13.48,81,78,82,83)

# Get named object
get("Reading0")

Output:

Error in get("Reading0") : object 'Reading0' not found

Here the output shows error because we try to retrive object which is not available.

Use get0() Function to Get Named Object Using Custom Message

Let’s see example how to use get0() function and pass custom message if object not found:

# Define vector
Reading <- c(12.39,9.25,14.15,13.48,81,78,82,83)

# Get named object
get0("Reading0",ifnotfound="not exist")

Output:

[1] "not exist"

The output shows custom message because we trying to retrive object which is not exist.

Use mget() Function to Retrive Multiple Named Objects

The following example show how to use mget() function to retrive multiple named objects:

# Define vector
Machine_name <- c("A","B","C","D","E","F","G","H")
Pressure <- c(12.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58)
Temperature <- c(78,89,85,84,81,79,77,85)

# Get multiple vector
mget(c("Machine_name","Pressure","Temperature"))

Output:

$Machine_name
[1] "A" "B" "C" "D" "E" "F" "G" "H"

$Pressure
[1] 12.39 11.25 12.15 13.48 13.78 12.89 12.21 12.58

$Temperature
[1] 78 89 85 84 81 79 77 85

Here in the above we successfully use mget() function to retrieve multiple vectors.

Common Mistakes to Avoid

Mistake 1: Forgetting quotes around the variable name

# ❌ WRONG - R looks for variable named "my_var"
result <- get(my_var)  # Error: object 'my_var' not found

# ✅ CORRECT - Variable name must be a string
result <- get("my_var")  # Works!

Mistake 2: Not handling missing objects with get()

# ❌ WRONG - Crashes if object doesn't exist
x <- get("nonexistent_var")  # Error: object 'nonexistent_var' not found

# ✅ CORRECT - Use get0() to provide fallback
x <- get0("nonexistent_var", ifnotfound = NA)  # Returns NA instead of error

Mistake 3: Not specifying the right environment

# ❌ WRONG - Looks in wrong environment
my_function <- function() {
  get("x")  # Looks in function environment, not global
}

# ✅ CORRECT - Specify environment explicitly
my_function <- function() {
  get("x", envir = .GlobalEnv)  # Look in global environment
}

Mistake 4: Mixing up mget() return format

# Remember - mget() returns a LIST, not individual variables
vars <- mget(c("a", "b", "c"))
# vars is a list: list(a = ..., b = ..., c = ...)
vars$a   # Access like a list element
vars[[1]] # Or by index

Pro Tips

  1. Prefer direct naming - Use my_object instead of get("my_object") when possible
  2. Document dynamic references - Code using get() can be hard to follow
  3. Use ls() to find variables - List all variables before using get()
  4. Combine with exists() - Check if variable exists before get():
    if (exists("my_var")) {
      x <- get("my_var")
    }
    

See Also