title: Using base::function with tidyverse and janitor to create several crosstables in R
body: <p>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:</p>
  <pre class="lang-r prettyprint-override"><code>library(tidyverse)
  library(janitor)
  
  survey &lt;- 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(&quot;No&quot;, NA, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;Yes&quot;, &quot;Yes&quot;)),
                   diagnosis_b = as.factor(c(&quot;Yes&quot;, &quot;Yes&quot;, &quot;No&quot;, NA, &quot;Yes&quot;, &quot;No&quot;, NA, &quot;Yes&quot;, &quot;Yes&quot;, &quot;Yes&quot;)))
  
  survey %&gt;% 
    filter(!is.na(diagnosis_a) &amp; !is.na(dimension_1)) %&gt;% 
    tabyl(dimension_1, diagnosis_a) %&gt;% 
    adorn_totals() %&gt;%
    adorn_percentages() %&gt;% 
    adorn_pct_formatting() %&gt;% 
    adorn_ns(position = &quot;front&quot;) %&gt;% 
    adorn_title(row_name = &quot;dimension_1&quot;,
                col_name = &quot;diagnosis_a&quot;)
  #&gt;              diagnosis_a           
  #&gt;  dimension_1          No        Yes
  #&gt;            1   2 (50.0%) 2  (50.0%)
  #&gt;            2   0  (0.0%) 3 (100.0%)
  #&gt;            3   1 (50.0%) 1  (50.0%)
  #&gt;        Total   3 (33.3%) 6  (66.7%)
  </code></pre>
  <p><sup>Created on 2022-01-05 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.1)</sup></p>
  <p>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:</p>
  <pre class="lang-r prettyprint-override"><code>xtable_fun &lt;- function(data, dimension, diagnosis) {
    data %&gt;% 
      filter(!is.na(.[diagnosis] &amp; !is.na(.[dimension]))) %&gt;% 
      tabyl(.[dimension], .[diagnose])
      adorn_totals() %&gt;%
      adorn_percentages() %&gt;% 
      adorn_pct_formatting() %&gt;% 
      adorn_ns(position = &quot;front&quot;) %&gt;% 
      adorn_title(row_name = dimension,
                  col_name = diagnosis)
  }
  
  xtable_fun(survey, &quot;dimension_1&quot;, &quot;diagnosis_a&quot;)
  </code></pre>
  <p><sup>Created on 2022-01-05 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.1)</sup></p>
  <p>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.</p>
  <p>Thank you for your help.</p>