Search code examples
rdplyrtidyeval

Using unquoted strings in with `dplyr` verbs: `select` and `arrange` working differently


I am trying to unquote a string for use in dplyr::arrange. It does not appear to work. However, it appears to work correctly in dplyr::select. Am I missing something or doing something wrong here?


library(tidyverse)

df <- tibble(x = c(1, 2, 3),
             y = c(8, 6, 3))

v <- 'y'

# `select` works with `!!v` 

df %>% select(y)
#> # A tibble: 3 x 1
#>       y
#>   <dbl>
#> 1     8
#> 2     6
#> 3     3

df %>% select(!!v)
#> # A tibble: 3 x 1
#>       y
#>   <dbl>
#> 1     8
#> 2     6
#> 3     3

# `arrange` not work with `!!v`

df %>% arrange(y)
#> # A tibble: 3 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1     3     3
#> 2     2     6
#> 3     1     8

df %>% arrange(!!v)
#> # A tibble: 3 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1     8
#> 2     2     6
#> 3     3     3

Solution

  • You need to convert your string to variable first (using sym()) then unquote it inside arrange().

    df %>% arrange(!!sym(v))
    
    #> # A tibble: 3 x 2
    #>       x     y
    #>   <dbl> <dbl>
    #> 1     3     3
    #> 2     2     6
    #> 3     1     8
    

    select() can directly take string input but it's not recommended

    df %>% select(v)
    
    #> Note: Using an external vector in selections is ambiguous.
    #> i Use `all_of(v)` instead of `v` to silence this message.
    #> i See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
    #> This message is displayed once per session.
    #> # A tibble: 3 x 1
    #>       y
    #>   <dbl>
    #> 1     8
    #> 2     6
    #> 3     3
    

    Created on 2020-11-21 by the reprex package (v0.3.0)