I used the answer to this question to create a function where I can use any number of variables in tab_cells. It works great until I try to put a multiple response set in the string I get an error Error: names not found: 'mdset(`4.2_1` %to% `4.2_13`)', 'mdset(`4.3_1` %to% `4.3_12`)'
connectionsvars <- "connected_scc, mdset(`4.2_1` %to% `4.2_13`), mdset(`4.3_1` %to% `4.3_12`)"
connectionsvar_names = trimws(unlist(strsplit(connectionsvars, split = ",")))
bannertable = function(varname, varnametitle) {
xtab = eval(substitute({
bannertable <- survey_data1 %>%
tab_cols(total(), age_range) %>%
tab_cells(eval(..[(varname)])) %>%
tab_stat_cpct(total_row_position = c("below"),
total_statistic = c("w_cpct"),
total_label = "#Total %") %>%
tab_pivot()
}))
assign(paste0(varnametitle, "Wtable"), bannertable, envir = .GlobalEnv)
}
bannertable(varname = connectionsvar_names, "connections")
How do I use multiple response sets while being able to use any number of variables in tab_cells?
If you want to make custom function and don't insist on incapsulated your variables in connectionsvars
, the simplest solution is to use dots as argument:
library(expss)
data(mtcars)
banner_table = function(varname_title, ...){
xtab = eval(substitute({
mtcars %>%
tab_cols(total(), am) %>%
tab_cells(...) %>%
tab_stat_cpct(total_row_position = c("below"),
total_statistic = c("w_cpct"),
total_label = "#Total %") %>%
tab_pivot()
}))
xtab
}
banner_table("First", vs, cyl)
banner_table("First", mdset(cyl %to% vs), am)