I am trying to run gather() on data frames, and programmatically assign the .key column name using !!quo(). But I keep getting 'Error: Invalid column specification'. I even found a closed ticket where it shows that it should work (https://github.com/tidyverse/tidyr/issues/293).
I'm going to go back to using rename_() as a workaround, but it would be nice to use the more elegant NSE.
library('tidyverse')
data(mtcars)
my_var <- 'my_col_name'
The following works, but is a one-trick pony
> mtcars %>%
as_tibble %>%
rownames_to_column('car_make') %>%
gather(my_col_name, values, -car_make)
# A tibble: 352 x 3
car_make my_col_name values
<chr> <chr> <dbl>
1 Mazda RX4 mpg 21.0
2 Mazda RX4 Wag mpg 21.0
3 Datsun 710 mpg 22.8
4 Hornet 4 Drive mpg 21.4
5 Hornet Sportabout mpg 18.7
6 Valiant mpg 18.1
7 Duster 360 mpg 14.3
8 Merc 240D mpg 24.4
9 Merc 230 mpg 22.8
10 Merc 280 mpg 19.2
# ... with 342 more rows
The following attempts to use tidyeval two give the same error:
> mtcars %>%
as_tibble %>%
rownames_to_column('car_make') %>%
gather(!!quo(my_var), values, -car_make)
Error: Invalid column specification
> mtcars %>%
as_tibble %>%
rownames_to_column('car_make') %>%
gather(!!enquo(my_var), values, -car_make)
Error: Invalid column specification
Library versions
tidyverse_1.1.1
dplyr_0.7.0
tidyr_0.6.3
rlang_0.1.1