I would like to export a list of objects and use their names as part of the path, but I am having trouble to ‘extract’ their names in a less manual manner. Here is the simplified example code that outlines the issue:
1. Create five sample data frames:
df <- map(1:5, (df) {
data.frame(x = rnorm(n = 5))
})
2. Name each element in the list:
names(df) <- c(paste0("df_", 1:5))
3. Save the elements into global environment:
list2env(x = df, envir = globalenv())
As solution from In R, how to get an object’s name after it is sent to a function?, extracting the object name as character for a single element works for me:
deparse(substitute(df_1))
I get:
> [1] "df_1"
But when I attempt to extract the object names for all element from the list with purrr::map()
, it doesn’t work:
map_chr(c(df_1, df_2, df_3, df_4, df_5), (.x) {
deparse(substitute(.x))
})
I get:
x x x x x
".x[[i]]" ".x[[i]]" ".x[[i]]" ".x[[i]]" ".x[[i]]"
I am not sure why this happens, and I’d appreciate comments from you. Thank you.