-1

I would like to use pmap with non standard evaluation.

I have tried with a small example but it does not work.

library(tidyverse)
library(magrittr)
#> 
#> Attachement du package : 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract

df <- tibble(
  x = 1:3 + 0.1,
  y = 3:1 - 0.1
)
test_func <- function(x, y){
    x + y
}
# Work:
df %>%
  mutate(
  test = pmap_dbl(
    list(x = x, y = y),
    test_func)
  )
#> # A tibble: 3 x 3
#>       x     y  test
#>   <dbl> <dbl> <dbl>
#> 1   1.1   2.9     4
#> 2   2.1   1.9     4
#> 3   3.1   0.9     4

# NSE does not work:
df %>%
  mutate(
  test = pmap_dbl(
    list(x = x, y = y),
    ~test_func(x = x, y = y))
  )
#> Error: Evaluation error: Result 1 is not a length 1 atomic vector.

Created on 2019-03-28 by the reprex package (v0.2.0.9000).

I expect that NSE output is the same than the "usual" equivalent of pmap.

  • I think you might be confused about what NSE is, you just mean "formula notation" here don't you ? see also : https://stackoverflow.com/questions/51122773/harnessing-f-list-names-with-purrrpmap/51123520#51123520 . If you think it's a duplicate, tell me and I'll close this one. – moodymudskipper Apr 01 '19 at 22:28
  • 1
    You are right, I was confused about NSE, I effectively meant formula notation. So, my question is clearly a duplicate of the one that you linked. Thank you very much – Alain Danet Apr 02 '19 at 11:19
  • happy to help ! – moodymudskipper Apr 02 '19 at 11:23

1 Answers1

0

Well, actually the documentation is pretty clear.

I need to evaluate arguments by referring to them as ..1, ..2, ..3, etc...

The order of the arguments apparently matters because the names of the list are not evaluated (But I would like to!).

So, for a more elaborated example, it gives:

library(tidyverse)
library(magrittr)
#> 
#> Attachement du package : 'magrittr'
#> The following object is masked from 'package:purrr':
#> 
#>     set_names
#> The following object is masked from 'package:tidyr':
#> 
#>     extract

df <- tibble(
  x = 1:3 + 0.1,
  y = 3:1 - 0.1,
  z = c("a", "b", "c")
)
test_func <- function(x, y, cond, ...){
  if (cond == "a") {
    x + y
  } else {
   NA 
  }
}
# Work:
df %>%
  mutate(
  test = pmap_dbl(
    list(x = x, y = y, cond = z),
    test_func)
  )
#> # A tibble: 3 x 4
#>       x     y z      test
#>   <dbl> <dbl> <chr> <dbl>
#> 1   1.1   2.9 a         4
#> 2   2.1   1.9 b        NA
#> 3   3.1   0.9 c        NA

# NSE does work as well:
df %>%
  mutate(
  test = pmap_dbl(
    list(x, y, z),
    ~test_func(x = ..1, y = ..2, cond = ..3))
  )
#> # A tibble: 3 x 4
#>       x     y z      test
#>   <dbl> <dbl> <chr> <dbl>
#> 1   1.1   2.9 a         4
#> 2   2.1   1.9 b        NA
#> 3   3.1   0.9 c        NA

# But the order of the arguments matters, the names of the list are apparently
# not evaluated: 
df %>%
  mutate(
  test = pmap_dbl(
    list(..3 = x, ..2 = y, ..1 = z),
    ~test_func(x = ..3, y = ..2, cond = ..1))
  )
#> # A tibble: 3 x 4
#>       x     y z      test
#>   <dbl> <dbl> <chr> <dbl>
#> 1   1.1   2.9 a        NA
#> 2   2.1   1.9 b        NA
#> 3   3.1   0.9 c        NA

Created on 2019-03-28 by the reprex package (v0.2.0.9000).