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:
get()
enquo() and !!
.env$var
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.
I'm writing a function wherein I have a user-defined grouping variable in my environment that I would like to inject into
t_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:
I've also tried:
get()
enquo()
and!!
.env$var
All give a version of the same error indicating the columns don'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!