0

有没有将 cols_label 中的列名作为字符串的解决方案?

fun <- function( df,column, label )
{
  df %>%
    gt() %>%
    cols_label( column = label)
  
}


fun( mtcars,"cyl", "cylinder")
4

1 回答 1

1

使用卷曲运算符。

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"))
于 2020-11-06T15:31:51.150 回答