I am trying to write a function that makes use of an object's name (as in unevaluated symbol) for downstream application. Here is an example that captures the sense:
return_obj_name <- function(obj){
inp <- enquo(obj)
inp_name <- rlang::as_name(inp) # Use the name for something
inp_data <- rlang::eval_tidy(inp) # This line just for completeness, not important here
return(inp_name)
}
Here is a standard use case of this function:
test_obj <- 42
return_obj_name(test_obj)
[1] "test_obj"
So far, so good. However, I plan to use my function as an anonymous function in a map (or map2) statement, and this is where things go wrong.
test_obj2 <- 44
test_vec <- c(test_obj, test_obj2)
map(test_vec, ~ .x %>% return_obj_name())
[[1]]
[1] "."
[[2]]
[1] "."
The intended output would have been:
[[1]]
[1] "test_obj"
[[2]]
[1] "test_obj2"
I think I do understand what is happening. The function receives the piped reference to the initial object, which would be ".". It quotes this with enquo and continues as by design.
I am wondering if there is a way to with evaluate the reference in the environment in which map is called, as opposed to within the map call, as is happening now.