1

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
  • Where did you find the closed ticket? `gather()` is in `tidyr`, not `dplyr` and I don't think the current published `tidyr` (0.6.3) uses the new `rlang` syntax that `dplyr` does. Maybe it works in the dev version. – MrFlick Jul 07 '17 at 14:51
  • Just realised I should have posted a link to the ticket: https://github.com/tidyverse/tidyr/issues/293 – Anil Sharma Jul 07 '17 at 14:53
  • Yeah, that's definitely not published to CRAN yet.If you want to use those features you'd have to install the development version of the package (which may possibly be unstable): `devtools::install_github("tidyverse/tidyr")` – MrFlick Jul 07 '17 at 14:55
  • Well that was easy. Should I just delete this question? – Anil Sharma Jul 07 '17 at 14:59
  • Up to you. There's probably a small window of time where this question will be relevant. Not sure when updated versions of tidyverse packages will be published. – MrFlick Jul 07 '17 at 15:00

0 Answers0