4
votes

I'm curious why the purrr::map_* function family, despite being a part of tidyverse, does not support quasiquotation by splice-unquoting its dots prior to evaluation of the mapped function?

library(tidyverse)
library(rlang)

set.seed(1)
dots <- quos(digits = 2L)

# this obviously won't work
purrr::map_chr(rnorm(5L), 
               ~ format(.x, !!!dots))
#> Error in !dots: invalid argument type

# I'm confused why this does not work
purrr::map_chr(rnorm(5L), 
               ~ format(.x, ...), 
               !!!dots)
#> Error in !dots: invalid argument type

# Finally, this works
eval_tidy(expr(
  purrr::map_chr(rnorm(5L), 
                 ~ format(.x, ...), 
                 !!!dots)
))
#> [1] "1.5"   "0.39"  "-0.62" "-2.2"  "1.1"

Created on 2019-01-31 by the reprex package (v0.2.0).

1

1 Answers

2
votes

I think the problem is format does not support tidy dots -- you can use exec to force a function to be able to use them:

library(tidyverse)
library(rlang)

set.seed(1)

nums <- rnorm(5L) #for some reason couldn't replicate your numbers
nums
#[1] -0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078

dots <- exprs(digits = 2L)

map_chr(nums, ~exec(format, .x, !!!dots))
#[1] "-0.63" "0.18"  "-0.84" "1.6"   "0.33"

You also need to use exprs not quos to capture the additional function arguments for this to work (not entirely sure why quos doesn't work here though, to be honest).