andrewsris
andrewsris

Reputation: 85

Error using curly-curly operator within a function

I am trying to create a function to run a Kruskal Wallis test on my data, but with different grouping categories.The function I am trying to create would be able to take in the name of the different grouping categories of interest. I am basing my code off of a similar problem as seen here:

R: Kruskal-Wallis test in loop over specified columns in data frame

I am trying to use the new curly-curly operator from the rlang 0.4 package.

https://www.tidyverse.org/articles/2019/06/rlang-0-4-0/

Here is an example of my issue:

library(dplyr)
library(broom)
set.seed(123)

df <- tbl_df(data.frame(
  group_cat1 = as.factor(rep(c("a", "b", "c"), each = 5)),
  group_cat2 = as.factor(rep(c("x", "y", "z"), each = 5)),
  var1   = runif(15, 0, 10),
  var2    = runif(15, 0, 10),
  var3   = runif(15, 0, 10)))

models_df <- df %>% 
  gather(variable, result, -group_cat1) %>% 
  group_by(variable) %>% 
  do(kruskal.test(x = .$result, g =.$group_cat1) %>% tidy())

This works well, however, if I try to generalize the problem by defining a function, I am unable to get it to work.

get_linear_model <- function(group_category) {
  df %>% 
    gather(variable, result, -{{group_category}}) %>% 
  group_by(variable) %>% 
  do(kruskal.test(x = .$result, g =.${{group_category}) %>% tidy())
}

I get the following error

Error: unexpected '{' in:
"  group_by(variable) %>% 
  do(kruskal.test(x = .$result, g =.${"

I believe there is something I'm missing in regards to environments or how to use this new curly-curly operator in general. I'm a bit lost, so any help would be greatly appreciated!

Upvotes: 1

Views: 426

Answers (1)

akrun
akrun

Reputation: 887501

For the second cases, we can use [[ and convert to character string as $ wouldn't work well

get_linear_model <- function(group_category) {

  df %>% 
    gather(variable, result, -{{group_category}}) %>% 
    group_by(variable) %>% 
    do(kruskal.test(x = .$result, 
       g = .[[rlang::as_name(enquo(group_category))]]) %>% 
            tidy())
    }

models_df2 <- get_linear_model(group_cat1)
identical(models_df, models_df2)
#[1] TRUE

In the OP's code, the error is due to the unbalanced braces

 ...
  do(kruskal.test(x = .$result, g =.${{group_category}) %>% tidy())
                                     ^^^            ^^^
                                      2              1

Upvotes: 1

Related Questions