这是一个可重现的示例
#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")
根据我的情况,我想动态传递 tab_cols(total(),vs,gear) 中的变量信息。因此,为了便于使用,假设我想评估以下功能:
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")
这显然是一个错误!我知道仅适用于单个参数的惰性评估。因此尝试了很多在多个论坛上搜索,但没有运气。
所以,一种很好的方法可能是:
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")
但我想用一个变量来实现这一点(这将具有字符串或向量形式的变量信息),因为该变量可能存储超过 3 或 4 列信息。