在这个问题Name output of pmap on tibble中,我问了如何正确命名pmap
呼叫的输出,得到了一个很好的答案,我想我明白了.
不过,当我进一步调查这种行为时,我不再确定我是否理解了内在原理:
library(purrr)
library(tibble)
params <- tibble(nm = LETTERS[1:2], x = 1:2, y = 2:3, z = 1:2)
## 1. setting names on the first argument gives the expected results
params %>%
mutate(nm = set_names(nm)) %>%
pmap(function(nm, x, y, ...) {
x + y
}) %>%
names()
# [1] "A" "B"
## 2. actually it does not need to be the first element
params %>%
mutate(nm = set_names(nm)) %>%
pmap(function(x, y, nm, ...) {
x + y
}) %>%
names()
# [1] "A" "B"
## 3. however, setting names on `x` does not work
params %>%
mutate(x = set_names(x, nm)) %>%
pmap(function(x, y, nm, ...) {
x + y
}) %>%
names()
# NULL
## 4. I thought maybe because they disappear during the addition `+`, but nope
params %>%
mutate(z = set_names(z, nm)) %>%
pmap(function(x, y, nm, ...) {
x + y
}) %>%
names()
# NULL
## 5. Ok maybe a catch all argument `...` prohibits this behaviour, but no
params %>%
mutate(z = set_names(z, nm)) %>%
pmap(function(x, y, nm, z) {
x + y
}) %>%
names()
# NULL
## 6. It seems that names on a character vector work even if not referenced directly
params %>%
mutate(nm = set_names(nm)) %>%
pmap(function(x, y, ...) {
x + y
}) %>%
names()
# [1] "A" "B"
有人能告诉我[p]map
电话中名字最终是如何确定的吗?