kassambara / rstatix

Pipe-friendly Framework for Basic Statistical Tests in R
https://rpkgs.datanovia.com/rstatix/
440 stars 50 forks source link

Cannot inject environmental variable into t_test() #177

Open ckfaber opened 1 year ago

ckfaber commented 1 year ago

I'm writing a function wherein I have a user-defined grouping variable in my environment that I would like to inject intot_test() (e.g., t_test({{var}} ~ {{groupvar}})), but I keep getting an error related to pull(), indicating the column cannot be found. I'm not an expert in data-masking and injection, but I've tried every variation that I can think of and continue to get the same issue.

Here is code to generate a toy dataset and the various approaches I have tried, along with the error messages:

groupvar <- "Treatment"
facetvar <- "Sex"
plotvar <- "BodyMass"

df <- tibble(Subject = as.factor(1:10), BodyMass = runif(10,min = 20, max = 30), Sex = as.factor(rep(c("M","F"),5)), Treatment = as.factor(rep(c("Control","Experimental"),each = 5)))

# This works: 
df_ttest <- df %>%
  group_by(Sex) %>% # group by variable that will be used to facet
  t_test(BodyMass ~ Treatment) %>% # 
  adjust_pvalue(method = "bonferroni") %>%
  add_significance() %>%
  add_xy_position(x = "Treatment")

# This breaks:
df_ttest <- df %>%
  group_by({{facetvar}}) %>% # group by variable that will be used to facet
  t_test({{plotvar}} ~ {{groupvar}}) %>% # 
  adjust_pvalue(method = "bonferroni") %>%
  add_significance() %>%
  add_xy_position(x = {{groupvar}})

I've also tried:

All give a version of the same error indicating the columns don't exist.

Error in `pull()`:
! Can't extract columns that don't exist.
✖ Column `{\n    {\n        groupvar\n    }\n}` doesn't exist.

R Version: 4.2.2 R Studio Version: 2022.07.2 Build 576 rstatix version: 0.7.1.999

I would really appreciate any suggestions as to where to go from here, as I'm hoping to automatically generate plots with summary statistics for many plots from a large dataset.

Thank you!

GegznaV commented 1 year ago

@ckfaber, this is not an rstatix issue so please, close it.


Still, I will help you. Construct a formula via as.formula() : as.formula(paste(plotvar, "~", groupvar))

Is this what you expect:

``` r library(rstatix) #> #> Attaching package: 'rstatix' #> The following object is masked from 'package:stats': #> #> filter groupvar <- "Treatment" facetvar <- "Sex" plotvar <- "BodyMass" df <- tibble( Subject = as.factor(1:10), BodyMass = runif(10,min = 20, max = 30), Sex = as.factor(rep(c("M","F"),5)), Treatment = as.factor(rep(c("Control","Experimental"), each = 5)) ) # This works: df_ttest <- df %>% group_by(Sex) %>% # group by variable that will be used to facet t_test(BodyMass ~ Treatment) %>% # adjust_pvalue(method = "bonferroni") %>% add_significance() %>% add_xy_position(x = "Treatment") df_ttest #> # A tibble: 2 × 15 #> Sex .y. group1 group2 n1 n2 statistic df p p.adj p.adj.signif #> #> 1 F Body… Contr… Exper… 2 3 1.97 2.48 0.162 0.324 ns #> 2 M Body… Contr… Exper… 3 2 -0.00905 2.69 0.993 1 ns #> # ℹ 4 more variables: y.position , groups , xmin , #> # xmax # This breaks: df_ttest_2 <- df %>% group_by({{facetvar}}) %>% # group by variable that will be used to facet t_test(as.formula(paste(plotvar, "~", groupvar))) %>% # adjust_pvalue(method = "bonferroni") %>% add_significance() %>% add_xy_position(x = {{groupvar}}) df_ttest_2 #> # A tibble: 1 × 15 #> `"Sex"` .y. group1 group2 n1 n2 statistic df p p.adj #> #> 1 Sex BodyMass Control Experimental 5 5 1.28 7.41 0.239 0.239 #> # ℹ 5 more variables: p.adj.signif , y.position , #> # groups , xmin , xmax ``` Created on 2023-05-15 with [reprex v2.0.2](https://reprex.tidyverse.org)

?