Hi,
Welcome to the RStudio community!
Based off a post I found here, I think this might be a way to accomplish this:
library(dplyr)
condition = T
test = iris %>%
{if(condition) group_by(., Species) else .} %>%
summarise(n = n())
test
#> # A tibble: 3 x 2
#> Species n
#> <fct> <int>
#> 1 setosa 50
#> 2 versicolor 50
#> 3 virginica 50
condition = F
test = iris %>%
{if(condition) group_by(., Species) else .} %>%
summarise(n = n())
test
#> n
#> 1 150
Created on 2021-07-01 by the reprex package (v2.0.0)
I'm not a big fan of this notation, so I would normally just split up the code in two steps.
PJ