I have the following program that calculates bootstrap results for a set of different values and produces plots of the results. It does the trick, but it requires that I name each of the functions in the list model_names <- list(test_fun = test_fun, next_fun = next_fun) , which seems like double work. Is there a way to extract the name of the function called from the list instead?
# Trying to name list to help with eventual file naming of graph
library(tidyverse)
#> Registered S3 method overwritten by 'rvest':
#> method from
#> read_xml.response xml2
library(rsample)
# Define functions to loop over
test_fun <- function(df, cut_off) {
mean((df$carb < cut_off))
}
next_fun <- function(df, cut_off) {
mean(df[df$carb > cut_off, ]$mpg)
}
# All runs use same data
set.seed(2)
df_bt <- bootstraps(mtcars, times = 2000, apparent = TRUE)
# List of functions to loop over
model_names <- list(test_fun = test_fun, next_fun = next_fun)
mtcars_bt <- map(model_names, function(f) {
map_df(1:7, function(z) {
df_bt %>%
mutate(ratio = map(splits, ~ {
df <- analysis(.x)
tibble(
term = "ratio",
estimate = f(df, z),
std.error = NA_real_
)
}
)) %>%
int_pctl(ratio) %>%
mutate(cut_off = z)
})
}
)
names(mtcars_bt) <- names(model_names)
# graph production
map(names(model_names), function(x) {
ggplot(data = mtcars_bt[[x]], aes(x = cut_off, y = .estimate)) +
geom_line() +
geom_ribbon(aes(ymin = .lower, ymax = .upper), alpha = 0.1)
ggsave(paste0(x, ".pdf"))
})
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image
#> [[1]]
#> NULL
#>
#> [[2]]
#> NULL
Thank you. Re-reading my own question, I can see that it was not very clear what I was trying to achieve, so let me try with one that is boiled down as much as possible.
If I name each element in my list, I can get the name of each list element using, for example, lmap, as follows:
What I am looking for is a way of getting the "called" element of the list without having to name each of them. This, for example, just gives my blanks (obviously because I have not named each element):
Is there a way of getting "mtcars" and "iris" from the list dfs <- list(mtcars, iris)? I know I have used data frames here, but I assume the same process would work for both data frames and functions
Could you give a boiled down example of what your ideal use-case is for having access to the names? That might help folks (and me!) understand where you're coming from, since the literal answer to your question
is 'no' --- applying str() to dfs shows that only the content of the objects is retained.
That is kind of what I expected, but thank you for your help.
The end goal is to do some calculations and then save a graph with the name of the data frame as the first part of the file name, say mtcars_my_graph.pdf and iris_my_graph.pdf. Naming each item is fine for smaller lists and I already have code that do what I want when names are available. I was looking at whether I could do without the naming in case I run into larger lists.
In that case, if you're creating graphs through functions, there are definitely ways of capturing object names. Here's an example with objects in the parent environment of the function (where strings short-circuit passing the objects themselves):
I was so focused on going from data frames/functions themselves to names that I completely forgot that it could make more sense to go from strings to data frames/functions.