0
votes

I'm trying to execute a function that uses the names of passed parameters with purrr::pmap. Unlike purrr::map (see below), pmap doesn't preserve these names. The below MWE captures the issue:

print_names <- function(x) {
  print(names(x))
}

namedVec <- c(nameA = "valueA")
purrr::map(list(namedVec), print_names)
# [1] "nameA"
# [[1]]
# [1] "nameA"
purrr::pmap(list(namedVec), print_names)
# NULL
# $nameA
# NULL
1

1 Answers

2
votes

Note that, in pmap, the .l argument needs to be a list of listed arguments, but in your function call it's just a list:

print_names <- function(x) {
  print(names(x))
}

namedVec <- c(nameA = "valueA")

purrr::map(list(namedVec), ~print_names(.))
#> [1] "nameA"
#> [[1]]
#> [1] "nameA"

purrr::pmap(list(list(namedVec)), print_names)
#> [1] "nameA"
#> [[1]]
#> [1] "nameA"

Created on 2018-10-07 by the reprex package (v0.2.1)