Skip to content

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

@alistaire47

Description

@alistaire47

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementwipwork in progress

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions