Reputation: 23
I already have a list contains all functions in dplyr by using this code
content <- mget(ls("package:dplyr"), inherits = TRUE)
dplyr_functions <- Filter(is.function, content)
The result I wanna get is just like the result of
names(dplyr_functions)
It will be a chr vector containing all function names in dplyr package.
But when I use map()
function, my code is like:
dplyr_name <- map_chr(dplyr_functions, names)
There is an error said,
"Result 1 must be a single string, not NULL of length 0"
So I just want to know what the error mean? How can I use map_chr
to get a vector containing all names in dplyr_functions?
Upvotes: 1
Views: 1964
Reputation: 13135
map
loop through the list element's content "value" e.g. dplyr_functions[[1]]
and so on, not through the element as in dplyr_functions[1]
, try both to see the difference. Hence names(dplyr_functions[[1]])
returns NULL
and map_chr
fails, while names(dplyr_functions[1])
returns %>%
and map_chr
could work.
So we can loop through the list index and subset using the 2nd method or use imap
which designed to loop through the list names.
library(purrr)
map_chr(seq_along(dplyr_functions), ~names(dplyr_functions[.x]))
#or
imap_chr(dplyr_functions, ~.y) %>% unname()
Upvotes: 2