Using additional arguments in a R function inside purrr::map_df have no effect

36 views Asked by At

I have a function that performs several statistical tests (default is wilcoxon rank sum test) and returns a dataframe where each row is a result.

library(rstatix)
library(tidyverse)

calculate_pval <- function(df, xvar, yvars, test = wilcox_test, ...){
  stat_test <- yvars %>%
    map(rlang::sym) %>%
    map(\(y) rlang::inject(!! y ~ !! rlang::ensym(xvar))) %>%
    map_df(~ test(.x, ..., data = df))
  
  return (stat_test)
} 

I tried to have the option to add additional parameters to the statistical test (e.g., alternative). However, this seems to have no effect:

data("ToothGrowth")
df <- ToothGrowth
> df %>% wilcox_test(len ~ supp)
# A tibble: 1 × 7
  .y.   group1 group2    n1    n2 statistic      p
* <chr> <chr>  <chr>  <int> <int>     <dbl>  <dbl>
1 len   OJ     VC        30    30      576. 0.0645
> df %>% wilcox_test(len ~ supp, alternative = "greater")
# A tibble: 1 × 7
  .y.   group1 group2    n1    n2 statistic      p
* <chr> <chr>  <chr>  <int> <int>     <dbl>  <dbl>
1 len   OJ     VC        30    30      576. 0.0322
> calculate_pval(df, supp, "len")
# A tibble: 1 × 7
  .y.   group1 group2    n1    n2 statistic      p
  <chr> <chr>  <chr>  <int> <int>     <dbl>  <dbl>
1 len   OJ     VC        30    30      576. 0.0645
> calculate_pval(df, supp, "len", alternative = "greater")
# A tibble: 1 × 7
  .y.   group1 group2    n1    n2 statistic      p
  <chr> <chr>  <chr>  <int> <int>     <dbl>  <dbl>
1 len   OJ     VC        30    30      576. 0.0645

Am I getting something wrong about ellipse (although I have used it multiple times), or is there an inherent problem with the way I implement it inside a map_df function?

0

There are 0 answers