I want to pull the name of a list element out as the text for a plot title within an lapply()
or map()
function.
The point is to self-identify a list elements name within the lapply or map call, rather then supply them as an additional argument. My real use case already has other arguments going into the function, and I am curious if there is a way around doing that because it would simplify things quite a bit.
Self Identify a list element within purrr::map or lapply Say I have a list
library(tidyverse)
my_list <- list(A = data.frame(level = factor(1:5),
value = rnorm(5)),
B = data.frame(level = factor(1:5),
value = rnorm(5)))
And a simple function to plot it
test_fun <- function(named_list) {
ggplot(named_list) +
geom_col(aes(level, value)) +
ggtitle(named_list)
}
And I want to do something like this:
purrr::map(my_list, test_fun)
...But have them titled "A" & "B".
The goal is to identify the name of elements "A" or "B" within map/lapply That way I can pass that name as the plot title without supplying them as another function argument.
I've tried these within the function:
names(named_list)
# &
attr(named_list, "name")
But they each will pull the names within element A or B, which is not the desired behavior. I want to be up one level.
I know that I could pass the names as a separate object with map2
my_names <- names(my_list)
test_fun2 <- function(named_list, my_names) {
ggplot(named_list) +
geom_col(aes(level, value)) +
ggtitle(my_names)
}
Then do this:
map2(my_list, my_names, test_fun2)
But is there a way to just insert the list element name instead of doing that? If there isn't that is fine, I am curious whether a way exists or if I'm stuck supplying the names.
EDIT: Looking for clarity on imap()
answer:
I can get the imap()
suggestions to work on my simple example but only with an anonymous function. If I could get some clarity on the below example I should be able to scale up to the more complicated use.
Why does this fail
test_fun_imap <- function(named_list) {
ggplot(.x) +
geom_col(aes(level, value)) +
ggtitle(.y)
}
imap(my_list, test_fun_imap)
But this works
imap(my_list, ~{
ggplot(.x) +
geom_col(aes(level, value)) +
ggtitle(.y)
}
)