有没有将 cols_label 中的列名作为字符串的解决方案?
像
fun <- function( df,column, label )
{
df %>%
gt() %>%
cols_label( column = label)
}
fun( mtcars,"cyl", "cylinder")
使用卷曲运算符。
library(gt)
fun <- function(df, column, label)
{
df %>%
gt() %>%
cols_label({{column}} := label)
}
fun(mtcars, "cyl", "cylinder")
或者,您可以使用该.list
参数。这将处理列向量。
library(gt)
fun <- function(df, column, label)
{
cols_list = as.list(label) %>% purrr::set_names(column)
df %>%
gt() %>%
cols_label(.list = cols_list)
}
fun(mtcars, "cyl", "cylinder")
fun(mtcars, c("cyl", "hp"), c("cylinder", "horsepower"))