Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple variable information inside R functions using string variable

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.

like image 620
nikki Avatar asked Jan 28 '23 07:01

nikki


2 Answers

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.

like image 172
Gregory Demin Avatar answered Jan 30 '23 21:01

Gregory Demin


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")
like image 22
ngm Avatar answered Jan 30 '23 20:01

ngm