I have a few package functions where I am using NSE, which works fine when the function internally uses only dplyr, but runs into issues when there is also tidyr involved. Since both of these packages belong to the tidyverse ecosystem, I had assumed that their implementation of NSE is similar.
Is this not true? Are the following differences expected? Or I am doing something wrong here?
Thanks.
setup
set.seed(123)
library(tidyverse)
df <- as.data.frame(Titanic)
# function definition
foo <- function(data, x) {
# dplyr
print(as_tibble(dplyr::select(data, {{x}})))
# tidyr
print(as_tibble(tidyr::uncount(data = data, weights = {{ x }})))
}
with symbol
Works as expected for both dplyr and tidyr.
foo(df, Freq)
#> # A tibble: 32 x 1
#> Freq
#> <dbl>
#> 1 0
#> 2 0
#> 3 35
#> 4 0
#> 5 0
#> 6 0
#> 7 17
#> 8 0
#> 9 118
#> 10 154
#> # ... with 22 more rows
#> # A tibble: 2,201 x 4
#> Class Sex Age Survived
#> <fct> <fct> <fct> <fct>
#> 1 3rd Male Child No
#> 2 3rd Male Child No
#> 3 3rd Male Child No
#> 4 3rd Male Child No
#> 5 3rd Male Child No
#> 6 3rd Male Child No
#> 7 3rd Male Child No
#> 8 3rd Male Child No
#> 9 3rd Male Child No
#> 10 3rd Male Child No
#> # ... with 2,191 more rows
with string
Works as expected with dplyr, but not tidyr.
foo(df, "Freq")
#> # A tibble: 32 x 1
#> Freq
#> <dbl>
#> 1 0
#> 2 0
#> 3 35
#> 4 0
#> 5 0
#> 6 0
#> 7 17
#> 8 0
#> 9 118
#> 10 154
#> # ... with 22 more rows
#> Error: `weights` must evaluate to a numeric vector
with a column name
Works as expected with dplyr, but not tidyr.
foo(df, names(df)[5])
#> # A tibble: 32 x 1
#> Freq
#> <dbl>
#> 1 0
#> 2 0
#> 3 35
#> 4 0
#> 5 0
#> 6 0
#> 7 17
#> 8 0
#> 9 118
#> 10 154
#> # ... with 22 more rows
#> Error: `weights` must evaluate to a numeric vector
with NULL
Works as expected for both dplyr and tidyr.
foo(df, NULL)
#> # A tibble: 32 x 0
#> # A tibble: 2,201 x 5
#> Class Sex Age Survived Freq
#> <fct> <fct> <fct> <fct> <dbl>
#> 1 3rd Male Child No 35
#> 2 3rd Male Child No 35
#> 3 3rd Male Child No 35
#> 4 3rd Male Child No 35
#> 5 3rd Male Child No 35
#> 6 3rd Male Child No 35
#> 7 3rd Male Child No 35
#> 8 3rd Male Child No 35
#> 9 3rd Male Child No 35
#> 10 3rd Male Child No 35
#> # ... with 2,191 more rows
Created on 2020-06-11 by the reprex package (v0.3.0)