You can use get() function to get named objects.
The following methods show how you can do it with syntax.
Method 1: Use get() Function
get("object_name")
Method 2: Use get0() Function
The get0() function is used to get named object if not found then show custom message.
get0("object_name",ifnotfound="custom message")
Method 3: Use mget() Function
The mget() function to get multiple objects.
mget(c("object1","object2","object3",..))
Here is few examples to understand this function in detailed:
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 succesfully use mget() function to retrive multiple vectors.