简体   繁体   English

使用`rlang :: exec`和使用`rlang :: ensym`的函数

[英]using `rlang::exec` with functions that use `rlang::ensym`

I am trying to write a custom function which is a bit more complicated so for the sake of simplicity I have created toy examples. 我正在尝试编写一个更复杂的自定义函数,因此为了简单起见,我创建了玩具示例。

Let's say I want to write a function that- 假设我想写一个函数 -

  1. automatically decides the appropriate function to run: for example, a t-test or an anova. 自动决定运行的相应功能:例如,t检验或anova。
  2. accepts both "quoted" and unquoted arguments 接受"quoted"和不unquoted参数

So I write a function to run a t-test (works as expected): 所以我编写了一个运行t检验的函数(按预期工作):

set.seed(123)
library(rlang)
library(tidyverse)

# t-test function
fun_t <- function(data, x, y) {
  # make sure both quoted and unquoted arguments work
  x <- rlang::ensym(x)
  y <- rlang::ensym(y)

  # t-test
  broom::tidy(stats::t.test(
    formula = rlang::new_formula({{ y }}, {{ x }}),
    data = data
  ))
}

# works fine
fun_t(mtcars, am, wt)
#> # A tibble: 1 x 10
#>   estimate estimate1 estimate2 statistic p.value parameter conf.low
#>      <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>
#> 1     1.36      3.77      2.41      5.49 6.27e-6      29.2    0.853
#> # ... with 3 more variables: conf.high <dbl>, method <chr>,
#> #   alternative <chr>

fun_t(mtcars, "am", "wt")
#> # A tibble: 1 x 10
#>   estimate estimate1 estimate2 statistic p.value parameter conf.low
#>      <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>
#> 1     1.36      3.77      2.41      5.49 6.27e-6      29.2    0.853
#> # ... with 3 more variables: conf.high <dbl>, method <chr>,
#> #   alternative <chr>

Then I write a function to run an anova (works as expected): 然后我编写一个函数来运行anova(按预期工作):

# anova function
fun_anova <- function(data, x, y) {
  # make sure both quoted and unquoted arguments work
  x <- rlang::ensym(x)
  y <- rlang::ensym(y)

  # t-test
  broom::tidy(stats::aov(
    formula = rlang::new_formula({{ y }}, {{ x }}),
    data = data
  ))
}

# works fine
fun_anova(mtcars, cyl, wt)
#> # A tibble: 2 x 6
#>   term         df sumsq meansq statistic      p.value
#>   <chr>     <dbl> <dbl>  <dbl>     <dbl>        <dbl>
#> 1 cyl           1  18.2 18.2        47.4  0.000000122
#> 2 Residuals    30  11.5  0.384      NA   NA

fun_anova(mtcars, "cyl", "wt")
#> # A tibble: 2 x 6
#>   term         df sumsq meansq statistic      p.value
#>   <chr>     <dbl> <dbl>  <dbl>     <dbl>        <dbl>
#> 1 cyl           1  18.2 18.2        47.4  0.000000122
#> 2 Residuals    30  11.5  0.384      NA   NA

Then I write a meta-function to choose the appropriate function from above- 然后我写一个元函数从上面选择适当的函数 -

fun_meta <- function(data, x, y) {
  # make sure both quoted and unquoted arguments work
  x <- rlang::ensym(x)
  y <- rlang::ensym(y)

  # which test to run?
  if (nlevels(data %>% dplyr::pull({{ x }})) == 2L) {
    .f <- fun_t
  } else {
    .f <- fun_anova
  }

  # executing the appropriate function
  rlang::exec(
    .fn = .f,
    data = data,
    x = x,
    y = y
  )
}

# using the meta-function
fun_meta(mtcars, am, wt)
#> Only strings can be converted to symbols

fun_meta(mtcars, "am", "wt")
#> Only strings can be converted to symbols

But this doesn't seem to work. 但这似乎不起作用。 Any ideas on what I am doing wrong here and how to get this to work? 关于我在这里做错了什么以及如何让它发挥作用的任何想法?

It seems like the problem is stemming from passing what amounted to, eg, x = rlang::ensym(am) to your individual functions via rlang::exec() in your meta function. 似乎问题源于通过元函数中的rlang::exec()将所得到的内容(例如x = rlang::ensym(am)传递给您的各个函数。

The ensym() function takes only strings or symbols, so doing this led to the error message. ensym()函数只接受字符串或符号,因此这样做会导致错误消息。 Given this, converting your x and y arguments to strings should help. 鉴于此,将xy参数转换为字符串应该会有所帮助。

So the meta function could be: 因此元函数可以是:

fun_meta <- function(data, x, y) {
     # make sure both quoted and unquoted arguments work
     x <- rlang::ensym(x)
     y <- rlang::ensym(y)

     # which test to run?
     if (dplyr::n_distinct(data %>% dplyr::pull({{ x }})) == 2L) {
          .f <- fun_t
     } else {
          .f <- fun_anova
     }

     # executing the appropriate function
     rlang::exec(
          .fn = .f,
          data = data,
          x = rlang::as_string(x),
          y = rlang::as_string(y)
     )
}

(I switched to n_distinct() from nlevels because am and cyl aren't factors and so I wasn't getting the right results to compare to your original results.) (我切换到n_distinct()nlevels因为amcyl并不因素,所以我没有得到正确的结果进行比较原始结果。)

Now using both bare symbols and strings work: 现在使用裸符号和字符串工作:

fun_meta(mtcars, am, wt)
    # A tibble: 1 x 10
  estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high
     <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl>
1     1.36      3.77      2.41      5.49 6.27e-6      29.2    0.853      1.86
# ... with 2 more variables: method <chr>, alternative <chr>
> fun_meta(mtcars, "am", "wt")

fun_meta(mtcars, "am", "wt")
# A tibble: 1 x 10
  estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high
     <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl>
1     1.36      3.77      2.41      5.49 6.27e-6      29.2    0.853      1.86
# ... with 2 more variables: method <chr>, alternative <chr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM