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"
?
到目前为止,我已经解决了一个非常棘手的解决方案:
names(names2)<-names(ft_1$header$dataset[1,])
ft_1$header$dataset[1,]<-names2
最佳答案
set_header_labels
用于标题值的较小修改。如果要使用一组一个或多个新标题行映射df列名,则可以使用set_header_df
:https://davidgohel.github.io/flextable/articles/layout.html#define-headers-with-a-reference-table
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