4

Here is a reproducible example

#install.packages("expss")
library("expss")
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)

mtcars %>%
  tab_cols(total(),vs,gear) %>%
  tab_cells(gear) %>% 
  tab_stat_cpct(total_row_position = "none", label = "col %") %>%
  tab_pivot(stat_position = "inside_rows") 

As per my situation i want to pass the information of variable in tab_cols(total(),vs,gear) dynamically. So for ease of use let say I would like to evaluate function like:

var1 <- "vs, gear"

mtcars %>%
  tab_cols(total(),var1) %>%
  tab_cells(gear) %>% 
  tab_stat_cpct(total_row_position = "none", label = "col %") %>%
  tab_pivot(stat_position = "inside_rows") 

which gives an error, obviously!! i knew the lazy evaluation which works for single parameter only. hence tried a lot to search on multiple forums but no luck.

so, one fine way could be:

var1 <- "vs"
var2 <- "gear"
mtcars %>%
  tab_cols(total(),eval(parse(text = var1)),eval(parse(text = var2))) %>%
  tab_cells(gear) %>% 
  tab_stat_cpct(total_row_position = "none", label = "col %") %>%
  tab_pivot(stat_position = "inside_rows") 

but I wanted to achieve this with a single variable (which would be having variable information either in a string or in vector form) as the variable might store more than 3 or 4 column information.

zx8754
  • 52,746
  • 12
  • 114
  • 209
nikki
  • 239
  • 1
  • 12

2 Answers2

5

There is a special facility in the expss to pass parameters:

var1 <- "vs, gear"
var_names = trimws(unlist(strsplit(var1, split = ","))) 

mtcars %>%
    tab_cols(total(), ..[(var_names)]) %>%
    tab_cells(gear) %>% 
    tab_stat_cpct(total_row_position = "none", label = "col %") %>%
    tab_pivot(stat_position = "inside_rows") 

Disclaimer: I am an author of the expss package.

Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • @Demin: Thanks a lot, that is what i am looking for. Much appreciated! – nikki Apr 25 '18 at 07:09
  • out of curiosity, this is a function which helped us in this case. would it be possible for you to share the basic trick to do that task, with which we can achieve this in other functions too.... – nikki Apr 26 '18 at 03:35
  • Can you please help in https://stackoverflow.com/q/50055988/9204376 topic – nikki Apr 27 '18 at 05:49
3

The documentation table_cols says you can pass a list of column names. So this seems to do what you want:

vars <- expression(list(vs, gear))

mtcars %>%
  tab_cols(total(), eval(vars)) %>%
  tab_cells(gear) %>% 
  tab_stat_cpct(total_row_position = "none", label = "col %") %>%
  tab_pivot(stat_position = "inside_rows")
ngm
  • 2,539
  • 8
  • 18
  • Thats also a way to pass multiple variable however in my case the car is in string. – ayush varshney Apr 17 '18 at 14:55
  • I don't understand. What car is in which string? – ngm Apr 17 '18 at 14:58
  • Oh you meant the variable ("var" mistyped as "car") is stored as a string? That didn't seem to be an essential part of your question. You want to send dynamically, in an interactive session (i.e. not in a function) multiple variables, and you tried a string but it didn't work. Well part of the issue is trying to use a string in the first place. But if you *have* to use a string (for reasons you haven't mentioned yet) then that becomes an essential part of the question, and you'll need to make an edit to reflect that. – ngm Apr 17 '18 at 15:03
  • sorry :( ....Yeah, i got it. that's my mistake that i won't be able to put this in my question... yes i am looking for an answer using a variable which will store information in either vector form or in string... – nikki Apr 17 '18 at 15:07