1

I'm looking for a way to automate table generation using the expss package in an attempt to move from spss to R. I think this should be simple but I seem to miss something...

I only define a few different tables based on the question type. Eg. the table for single respons looks like below

banner <- d %>% tab_cols(total(),Q2.banner,Q3.banner)
banner %>% 
tab_cells (Q1) %>%
tab_stat_cases(total_row_position = c("above"),label = 'N') %>%
tab_stat_cpct(total_row_position = c("none"), label = '%') %>%
tab_pivot (stat_position = "inside_rows") %>%  
drop_c ()  %>%
custom_format()

I'm looking for a function in which I only have to specify the variable Eg .

Table1 = function (Q, banner) {
 banner %>%
 tab_cells (Q) %>%
 tab_stat_cases(total_row_position = c("above"),label = 'N') %>%
 tab_stat_cpct(total_row_position = c("none"), label = '%') %>%
 tab_pivot (stat_position = "inside_rows") %>%  
 drop_c ()  %>%
 custom_format()
}

Ideally I would like to add a table title as well. I'm running the table books in R Notebook.

Any other tips to automate table generation are all welcome.

Thanks for all help, michaƫla

1 Answers1

0

There is a rather universal solution for working with non-standard evaluation - eval.parent(substitute(...)). In your case it looks like this:

Table1 = function (Q, banner) {
    eval.parent(substitute(
        {
            banner %>%
                tab_cells (Q) %>%
                tab_stat_cases(total_row_position = c("above"),label = 'N') %>%
                tab_stat_cpct(total_row_position = c("none"), label = '%') %>%
                tab_pivot (stat_position = "inside_rows") %>%  
                drop_c ()  %>%
                custom_format()
        }
    ))
}
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20