简体   繁体   English

有没有办法通过按顺序列出新的 header 名称来指定灵活表 header 标签?

[英]Is there a way to specify flextable header labels by just listing the new header names in order?

I'm using flextable with a dataset that has many columns, and I want to use set_header_labels() to change them all at once, in order, without having to specify which name goes with each specific value.我正在将 flextable 与具有许多列的数据集一起使用,并且我想使用 set_header_labels() 按顺序一次更改它们,而不必指定每个特定值对应的名称。 However, my code keeps running but not working.但是,我的代码继续运行但无法正常工作。 For instance, this code works:例如,此代码有效:

library(flextable)
ft <- flextable( head( iris ))
ft <- set_header_labels(ft,
  values = list(Sepal.Length = "Sepal length",
                Sepal.Width = "Sepal width",
                Petal.Length = "Petal length",
                Petal.Width = "Petal width" ) )
ft

But this code doesn't change anything:但是这段代码并没有改变任何东西:

library(flextable)
ft <- flextable( head( iris ))
values <- c("SEPAL LENGTH", "SEPAL WIDTH", "PETAL LENGTH", "PETAL WIDTH")

ft <- set_header_labels(ft,
                        values = values)
ft

I'd like to just change the names in the dataframe, but that's not an option because in my actual table, the headers will have duplicate names (ie, "2020", "2021", "2020", "2021", ...), which R won't allow.我只想更改 dataframe 中的名称,但这不是一个选项,因为在我的实际表中,标题将具有重复的名称(即“2020”、“2021”、“2020”、“2021”、. ..),R 不允许。

I see two suggestions for that.我看到了两个建议。

You can use set_header_df if you can provide a data.frame where colnames are associated to labels.如果您可以提供 colnames 与标签相关联的 data.frame,则可以使用set_header_df

library(flextable)
# suggestion 1: requires all names ----
ref_table <- data.frame(
  key = colnames(iris),
  label = c("SEPAL LENGTH", "SEPAL WIDTH", "PETAL LENGTH", "PETAL WIDTH", "SPECIES")
)
ft <- flextable( head( iris ))
ft <- set_header_df(ft, mapping = ref_table, key = "key")
ft <- theme_booktabs(ft)
ft

在此处输入图像描述

Or you can use setNames or names<- to make sure set_header_labels do its job (it's done by matching names (colkeys) and associated labels.或者您可以使用setNamesnames<-来确保set_header_labels完成它的工作(它是通过匹配名称(colkeys)和相关标签来完成的。

# suggestion 2: does not requires all names  ----
values <- setNames(c("SEPAL LENGTH", "SEPAL WIDTH", "PETAL LENGTH", "PETAL WIDTH"),
         colnames(iris)[-5]
)
ft <- flextable( head( iris ))
ft <- set_header_labels(ft, values = values)
ft

在此处输入图像描述

From the docs the values argument has to be从文档中, values参数必须是

a named list (names are data colnames), each element is a single character value specifying label to use.一个命名列表(名称是数据列名),每个元素都是一个字符值,指定要使用的 label。

One option to achieve your desired result would be to make use of setNames .实现所需结果的一种选择是使用setNames

Additionally I make use of ft$col_keys[seq(length(values)) to get the column names of your ft table object.此外,我使用ft$col_keys[seq(length(values))来获取ft表 object 的列名。

library(flextable)

values <- c("SEPAL LENGTH", "SEPAL WIDTH", "PETAL LENGTH", "PETAL WIDTH")

ft <- flextable(head(iris))
ft <- set_header_labels(ft,
  values = setNames(values, ft$col_keys[seq(length(values))])
)
ft

Created on 2021-12-14 by the reprex package (v2.0.1)reprex package (v2.0.1) 于 2021 年 12 月 14 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM