[R] Accessing an object using a string
G.Maubach at weinwolf.de
G.Maubach at weinwolf.de
Mon Aug 15 10:49:24 CEST 2016
Hi All,
I would like to access an object using a sting.
# Create example dataset
var1 <- c(1, 2, 3)
var2 <- c(4, 5, 6)
data1 <- data.frame(var1, var2)
var3 <- c(7, 8, 9)
var4 <- c(10, 11, 12)
data2 <- data.frame(var3, var4)
save(file = "c:/temp/test.RData", list = c("data1", "data2"))
# Define function
t_load_dataset <- function(file_path,
file_name) {
file_location <- file.path(file_path, file_name)
print(paste0('Loading ', file_location, " ..."))
cat("\n")
object_list <- load(file = file_location,
envir = .GlobalEnv)
print(paste(length(object_list), "dataset(s) loaded from",
file_location))
cat("\n")
print("The following objects were loaded:")
print(object_list)
cat("\n")
for (i in object_list) {
print(paste0("Object '", i, "' in '", file_name, "' contains:"))
str(i)
names(i) # does not work
}
}
I have only the character vector object_list containing the names of the
objects as strings. I would like to access the objects in object_list to
be able to print the names of the variables within the object (usuallly a
data frame).
Is it possible to do this? How is it done?
Kind regards
Georg
More information about the R-help
mailing list