Hello all,
I was trying to write a function to streamline my 'tabyl' function calls using the following syntax:
tab_freq <- function(data, v1, v2) {
data %>%
tabyl(v1, v2) %>%
adorn_totals(c("row", "col")) %>%
adorn_percentages("all") %>%
adorn_pct_formatting(2) %>%
adorn_ns()
}
tab_freq(df, var1, var2)
But I kept on getting the error: Error in .f(.x[[i]], ...) : object 'var1' not found
I then found a solution on the RStudio thread here: Problem for loop with Janitor and Rlang
It used curly braces to reference the variables v1 and v2 within the function:
tab_freq <- function(data, v1, v2) {
data %>%
tabyl({{ v1 }}, {{ v2 }}) %>%
adorn_totals(c("row", "col")) %>%
adorn_percentages("all") %>%
adorn_pct_formatting(2) %>%
adorn_ns() # no need to print here
}
I was definitely happy to find the solution. However, I have found no references or documentation for this {{}} convention anywhere on the web. Could someone please refer me to any documentation on referencing variables in this way? So far the attached web link is the only place I've seen this.
Thank you in advance!
RJ