creating summary for more than one variable

I am trying create a function for passing more that one variables for cro_cpct table against the banner. but not able to parse the list of variables. is there any solution of parsing the list of variables.

collist can have more that one variables.

data <-  mtcars
collist <-  c("drat","wt")
data$available <- ifelse(data$vs == 1,1,NA)
data$source <- ifelse(data$am == 1,1,NA)


expss::val_lab(data$available)<-c("Availability"=1)
expss::val_lab(data$source)<-c("sourcing"=1)

banner<-list(data$available,data$source)

df <- data[collist] %>% as.data.frame()
expss::var_lab(colnames(data)[ncol(data)]) <- ""

tabl <- expss::cro_cpct(expss::mrset(rlang::parse_expr(df[[collist]])), list(banner))

See the FAQ: How to do a minimal reproducible example reprex for beginners. val_lab and cro_cpct functions are missing.

Just Updated the question

This problem carries three lessons.

  1. Troubleshooting an error message can be done from the inside out to find the root cause dependably.
  2. The more involved a function the more likely it is to produce errors. Simpler is generally better.
  3. Posing a problem can be more difficult than solving it. In keeping with the functional nature of R programing, thinking of it in terms of what, rather than how can be helpful. What is wanted is a a return value from expss::cro_cpct for two variables. Doing so would have led to ??cro_cptc where the first positional argument is a

vector/data.frame/list. Variables on which percentage/cases will be computed. Use mrset/mdset for multiple-response variables.

with the additional information that mrset can be used for more than one variable. That, in turn, leads to ??mrset where the \dots argument is variables, which is simply a vector of variable names.

Add a new line at the top

require(expss)

The error from the last line is

Error in .subset2(x, i, exact = exact) : subscript out of bounds

which means that at least banner is deeply nested and more [[ extractors are needed.

> list(banner)
[[1]]
[[1]][[1]]
VALUES:
NA, NA, 1, 1, NA, 1, NA, 1, 1, 1, 1, NA, NA, NA, NA, NA, NA, 1, 1, 1, 1, NA, NA, NA, NA, 1, NA, 1, NA, NA, NA, 1
VALUE LABELS:               
 1 Availability

[[1]][[2]]
VALUES:
1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1, 1
VALUE LABELS:           
 1 sourcing

It appears, however, that

expss::cro_cpct(expss::mrset(rlang::parse_expr(df[[collist]])

is the culprit.

Changing that to

expss::cro_cpct(expss::mrset(rlang::parse_expr(df[collist])

substitutes a new error

Error: `x` must be a character vector or an R connection

from rlang::parse_expr, and that function is receiving the df[collist]] data frame as its argument, which is neither a character vector nor an R connection containing text as a character vector. What would work as an argument is

rlang::parse_expr("Text"))

which simply returns Text. That renders parse_expr superfluous and

expss::cro_cpct(expss::mrset("Text"))

would render, as would

tabl <- expss::cro_cpct(expss::mrset(df[collist]))

and adding banner also works

tabl <- expss::cro_cpct(expss::mrset(df[collist]), banner)
require(expss)
#> Loading required package: expss
data <-  mtcars
collist <-  c("drat","wt")
data$available <- ifelse(data$vs == 1,1,NA)
data$source <- ifelse(data$am == 1,1,NA)

expss::val_lab(data$available)<-c("Availability"=1)
expss::val_lab(data$source)<-c("sourcing"=1)

banner<-list(data$available,data$source)

df <- data[collist] %>% as.data.frame()
expss::var_lab(colnames(data)[ncol(data)]) <- ""

tabl <- expss::cro_cpct(expss::mrset(df[collist]), banner)
tabl
Availability sourcing
1.513 7.1 7.7
1.615 7.1 7.7
1.835 7.1 7.7
1.935 7.1 7.7
2.14 7.7
2.2 7.1 7.7
2.32 7.1 7.7
2.62 7.7
2.77 7.7
2.465 7.1
2.76 7.1
2.78 7.1 7.7
2.875 7.7
3.17 7.7
3.54 7.7
3.57 7.7
3.62 7.7
3.08 7.1
3.15 7.1
3.19 7.1
3.215 7.1
3.44 14.3
3.46 7.1
3.69 7.1
3.7 7.1
3.77 7.1 7.7
3.85 7.1 7.7
3.9 15.4
3.92 21.4
4.08 14.3 15.4
4.11 7.1 7.7
4.22 7.1 15.4
4.43 7.7
4.93 7.1 7.7
#Total cases 14 13

Great explanation of every single points , learned alot from this single question.
but when i am applying the same to my original data can you guess why i am getting this below error

debug at #8: tabl <- expss::cro_cpct(expss::mrset(data1[col_list]), banner)
Browse[2]> n
Error: 'cro': all variables should be of the same length or length 1.

so actually data1 is subset and have 20 records but banner is original raw records 98
so while applying common banner to this cro_cpct produced an error

any suggestion how can i keep banner
this function is common for all