Reputation: 1381
When using the following code:
get_objects <- function() {
x1 <- 123
x2 <- 23535
x_objects <- ls(pattern = 'x')
print(x_objects)
x_objects_list <- purrr::map(x_objects, get)
return(x_objects_list)
}
f <- get_objects()
I get the following error:
Error in .f(.x[[i]], ...) : object 'x1' not found
I suspect it has something to do with the scoping or environment, as when the object is globally defined, instead of in the function, I can use the code by evaluating
x_objects_list <- purrr::map(x_objects, get)
directly in the console. The reason is that I want a list of dataframes with a certain name so I can iteratively perform actions on them.
Upvotes: 2
Views: 748
Reputation: 13680
Not sure what you are trying to do, but as you do not share you bigger scope, this should solve you current issue:
get_objects <- function() {
x1 <- 123
x2 <- 23535
x_objects <- ls(pattern = 'x')
x_objects_list <- purrr::map(x_objects, get, envir = sys.frame(sys.parent(0)))
return(x_objects_list)
}
f <- get_objects()
Upvotes: 1