Reputation: 1367
I'm trying to call the same function multiple times on the same data but with different function arguments. My problem can be described as:
x <- as.character(1:5)
l <- list(list(name = "a", collapse = ""), list(name = "b", collapse = "-"))
output <- list()
for(l_cur in l) {
output[[l_cur$name]] <- x %>% paste(collapse = l_cur$collapse)
}
Is there any cleaner way to do it using purrr
?
(Background: I want to use this with rvest
because I'm calling html_nodes()
on the same data multiple times but I'm only changing css
argument.)
Upvotes: 0
Views: 151
Reputation: 388982
Probably , you can use :
library(purrr)
map(l, ~x %>% paste(collapse = .x$collapse)) %>% set_names(map(l, pluck, "name"))
#$a
#[1] "12345"
#$b
#[1] "1-2-3-4-5"
Upvotes: 1