7

帮助详细信息将flextable添加新标头描述为针对每个新标头名称手动键入每个现有标头名称,如下所示:

library(flextable)
ft_1 <- regulartable(head(iris))
ft_1 <- set_header_labels(ft_1, Sepal.Length = "SL",
                          Sepal.Width = "SW", Petal.Length = "PL",
                          Petal.Width = "PW"
)
ft_1

如何从字符串中添加所有新的标题名称,例如

(names2<-c('SL','SW','PL','PW','SPECIES'))
[1] "SL"      "SW"      "PL"      "PW"      "SPECIES"

?

到目前为止,我已经管理了一个非常 hacky 的解决方案:

names(names2)<-names(ft_1$header$dataset[1,])

ft_1$header$dataset[1,]<-names2
4

2 回答 2

6

set_header_labels用于对标头值进行细微修改。set_header_df如果您想使用一组一个或多个新标题行映射 df 列名,可以使用:https ://davidgohel.github.io/flextable/articles/layout.html#define-headers-with-a-reference-桌子

library(flextable)

names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ) )
ft <- set_header_df(x = ft, mapping = data.frame(keys = names1, values = names2, stringsAsFactors = FALSE),
              key = "keys" )

# the following call is needed as header formats have been 
# replaced by vanilla formats when set_header_df() has been called
ft <- theme_booktabs(ft) 
ft

在此处输入图像描述

于 2018-11-20T08:02:13.007 回答
1

好的,经过几次编辑,我希望这对你有用。

你的方法似乎也很好。

names1 <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")
names2 <- c('SL','SW','PL','PW','SPECIES')

ft <- flextable( head( iris ),
                   col_keys = names1 )

oldHeaders <- names1
newHeaders <- names2

headerlist <- setNames(as.list(newHeaders), 
                       oldHeaders)

ft <- do.call(set_header_labels, c(list(x = ft, top = F), headerlist))
ft
于 2018-11-20T06:35:48.020 回答