New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Pass fct_relabel fun parameter to rlang::as_function #91

Closed
alistaire47 opened this Issue Jun 3, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@alistaire47
Copy link
Contributor

alistaire47 commented Jun 3, 2017

It would be handy to pass the fun parameter of fct_relabel to rlang::as_function so it can accept formula (~tolower(.x)) or quosure (quo(tolower(.x)) style functions or string function names ("tolower") instead of just raw functions.

A trivial reprex (though realistically regex is the obvious use-case):

library(tidyverse)
library(forcats)

# Right now this is the only option for an anonymous function:
iris %>% 
    tbl_df() %>% 
    mutate(Species = fct_relabel(Species, 
                                 function(x){substr(x, 1, 3)}))
#> # A tibble: 150 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl>  <fctr>
#>  1          5.1         3.5          1.4         0.2     set
#>  2          4.9         3.0          1.4         0.2     set
#>  3          4.7         3.2          1.3         0.2     set
#>  4          4.6         3.1          1.5         0.2     set
#>  5          5.0         3.6          1.4         0.2     set
#>  6          5.4         3.9          1.7         0.4     set
#>  7          4.6         3.4          1.4         0.3     set
#>  8          5.0         3.4          1.5         0.2     set
#>  9          4.4         2.9          1.4         0.2     set
#> 10          4.9         3.1          1.5         0.1     set
#> # ... with 140 more rows

# ...but this syntax would be convenient and consistent with the rest of the tidyverse:
iris %>% 
    tbl_df() %>% 
    mutate(Species = fct_relabel(Species, 
                                 ~substr(.x, 1, 3)))
#> Error in mutate_impl(.data, dots): Evaluation error: Expected function, got formula.

# ...and it would work:
iris %>% 
    tbl_df() %>% 
    mutate(Species = fct_relabel(Species, 
                                 rlang::as_function(~substr(.x, 1, 3))))
#> # A tibble: 150 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl>  <fctr>
#>  1          5.1         3.5          1.4         0.2     set
#>  2          4.9         3.0          1.4         0.2     set
#>  3          4.7         3.2          1.3         0.2     set
#>  4          4.6         3.1          1.5         0.2     set
#>  5          5.0         3.6          1.4         0.2     set
#>  6          5.4         3.9          1.7         0.4     set
#>  7          4.6         3.4          1.4         0.3     set
#>  8          5.0         3.4          1.5         0.2     set
#>  9          4.4         2.9          1.4         0.2     set
#> 10          4.9         3.1          1.5         0.1     set
#> # ... with 140 more rows

The tradeoff for the convenience is that it requires adding rlang as a dependency. If that's ok, I'd be happy to assemble a PR if you like.

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Feb 10, 2018

A PR would be awesome!

(And we need to add rlang for #110, so it's not a big deal)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment