rtidyversejanitor

Using base::function with tidyverse and janitor to create several crosstables in R


I have a Data Frame with Survey Data and want to create crosstables between every dimension and diagnosis. I succesfully did this with dplyr and janitor:

library(tidyverse)
library(janitor)

survey <- tibble(dimension_1 = c(1, NA, 2, 3, 1, 1, 1, 3, 2, 2),
                 dimension_2 = c(2, 2, 3, 1, 1, 2, 2, 3, 1, 1),
                 diagnosis_a = as.factor(c("No", NA, "Yes", "No", "Yes", "Yes", "No", "Yes", "Yes", "Yes")),
                 diagnosis_b = as.factor(c("Yes", "Yes", "No", NA, "Yes", "No", NA, "Yes", "Yes", "Yes")))

survey %>% 
  filter(!is.na(diagnosis_a) & !is.na(dimension_1)) %>% 
  tabyl(dimension_1, diagnosis_a) %>% 
  adorn_totals() %>%
  adorn_percentages() %>% 
  adorn_pct_formatting() %>% 
  adorn_ns(position = "front") %>% 
  adorn_title(row_name = "dimension_1",
              col_name = "diagnosis_a")
#>              diagnosis_a           
#>  dimension_1          No        Yes
#>            1   2 (50.0%) 2  (50.0%)
#>            2   0  (0.0%) 3 (100.0%)
#>            3   1 (50.0%) 1  (50.0%)
#>        Total   3 (33.3%) 6  (66.7%)

Created on 2022-01-05 by the reprex package (v2.0.1)

Right now I have to copy the codechunk for every combination and wanted to create a function to just pass my variables as arguments. Unfortunately I wasn't able to make it work. Here's one example of what I tried:

xtable_fun <- function(data, dimension, diagnosis) {
  data %>% 
    filter(!is.na(.[diagnosis] & !is.na(.[dimension]))) %>% 
    tabyl(.[dimension], .[diagnose])
    adorn_totals() %>%
    adorn_percentages() %>% 
    adorn_pct_formatting() %>% 
    adorn_ns(position = "front") %>% 
    adorn_title(row_name = dimension,
                col_name = diagnosis)
}

xtable_fun(survey, "dimension_1", "diagnosis_a")

Created on 2022-01-05 by the reprex package (v2.0.1)

I guess the error is in the way I use the arguments but I never tried using my own functions before and can't find the solution.

Thank you for your help.


Solution

  • You were very close. Try using .data[[ to reference the variables. There were also a few typos:

    1. Missing closing )
    2. Missing %>% operator
    3. Wrong variable name.
    xtable_fun <- function(data, dimension, diagnosis) {
      data %>% 
        filter(!is.na(.data[[diagnosis]]) & !is.na(.data[[dimension]])) %>% 
        tabyl(.data[[dimension]], .data[[diagnosis]]) %>% 
        adorn_totals() %>%
        adorn_percentages() %>% 
        adorn_pct_formatting() %>% 
        adorn_ns(position = "front") %>% 
        adorn_title(row_name = dimension,
                    col_name = diagnosis)
    }
    
    xtable_fun(survey, "dimension_1", "diagnosis_a")
    

    Which gives as expected.

                 diagnosis_a           
     dimension_1          No        Yes
               1   2 (50.0%) 2  (50.0%)
               2   0  (0.0%) 3 (100.0%)
               3   1 (50.0%) 1  (50.0%)
           Total   3 (33.3%) 6  (66.7%)