3

So I'm trying to make cross tables for multi-response questions with both frequency and counts using expss. I am able to get the result I need by running the following code:

library(expss)
set.seed(1998)
expss_output_viewer()
# Example data set
x <- c("A","B","C")
area <-  rep(x,each = 10)
p <-  sample(c(0,1),30,replace = T)
q <-  sample(c(0,1),30,replace = T)
r <-  sample(c(0,1),30,replace = T)
mrdata <-  data.frame(area,p,q,r)
# Creating the Table
mrdata %>%
  tab_significance_options(keep = "none", sig_labels = NULL, subtable_marks = "greater", mode = "append") %>%
  tab_cols(total(), mdset(p,q,r)) %>%
  tab_cells(area) %>%
  tab_stat_cases(label = "cases") %>%
  tab_stat_cpct_responses(label = "%",total_row_position = "none") %>% 
  tab_pivot(stat_position = "inside_columns") %>% set_caption("Table 1")

However, seeing as this is a lot of code for a single table, I wanted to wrap it into a function to be able to create the tables quickly and without much clutter. I've tried doing it like this:

mrtable <-  function(input,rowvar,colvars,capt ="Table 1") {
  input %>%
    tab_significance_options(keep = "none", sig_labels = NULL, subtable_marks = "greater", mode = "append") %>%
    tab_cols(total(), mdset(colvars)) %>%
    tab_cells(rowvar) %>%
    tab_stat_cases(label = "cases") %>%
    tab_stat_cpct_responses(label = "%",total_row_position = "none") %>% 
    tab_pivot(stat_position = "inside_columns") %>% set_caption(capt)
}
mrtable(input = mrdata,colvars = c(p,q,r),rowvar = area)

Running the function above returns:

Error: 'cro': all variables should be of the same length or length 1.

I can't figure out why it fails. Any help would be appreciated.

EDIT: got it to work :

mrtable <-  function(input,rowvar,...,capt ="Table 1") {
  input %>%
    tab_significance_options(keep = "none", sig_labels = NULL, subtable_marks = "greater", mode = "append") %>%
    tab_cols(total(), mdset(...)) %>%
    tab_cells(rowvar) %>%
    tab_stat_cases(label = "cases") %>%
    tab_stat_cpct_responses(label = "%",total_row_position = "none") %>% 
    tab_pivot(stat_position = "inside_columns") %>% set_caption(capt)
}
mrtable(input = mrdata,rowvar = area,p,q,r,capt = "Tab")
  • The problem is that `expss` uses non-standard evaluation. That means, for example, that `mdset(p,q,r)` in your original code is completely different from `mdset(colvars)` in your function in a way that depends on what `expss` is doing, and I don't know that package. – user2554330 Jun 25 '22 at 19:35
  • @user2554330 ok so if expss used standard evaluation , then the function should have worked, right? is the way i specified the arguments in the function ok? – Ahmad Noman Alnoor Jun 25 '22 at 19:38
  • No, in standard evaluation `mdset(p,q,r)` is a function call with three arguments, while `mdset(colvars)` is a function call with one argument. You constructed `colvars` as `c(p,q,r)` which is what you get by concatenating variables `p, q, r` into a single vector. That's quite different than treating them as three different variables. – user2554330 Jun 25 '22 at 19:43
  • @user2554330 ok so, ideally, how should one specify arguments like these in the functions? – Ahmad Noman Alnoor Jun 26 '22 at 02:00
  • nevermind, i got it to work. updated my question – Ahmad Noman Alnoor Jun 26 '22 at 02:05
  • You can post your solution as an answer to the question if you want. – user2554330 Jun 26 '22 at 07:59

0 Answers0