Dynamically set colnames from character string in flextable
Asked Answered
K

2

7

The flextable help details describes adding new headers as manually typing each existing header name against each new header name, like this:

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

How can I just add all new header names from a character string such as

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

?

So far I have managed a very hacky solution:

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

ft_1$header$dataset[1,]<-names2
Karolyn answered 20/11, 2018 at 3:54 Comment(5)
What about changing the colnames in the dataset before converting it to a flextable?Rash
@user10626943 thanks for the comment. This is to add a second header, in addition to the colnames of the dataframe.Karolyn
Hopefully @davidgohel sees this and responds with something good. He's usually pretty responsive an helpful :) But I've always had trouble getting ft to work as I think it should :PRash
@user10626943 yes I noticed he is very helpful whilst searching through old SO questions on ft.Karolyn
:) yes, I am receiving alerts when tags officer/flextable are used and I am trying to answer when I can.Murky
M
6

set_header_labels is made for minor modifications of header values. You can use set_header_df if you want to map df column names with a set of one or more new header rows: 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

enter image description here

Murky answered 20/11, 2018 at 8:2 Comment(1)
Thanks for a prompt answer and a great package!Karolyn
K
1

OK, after a couple edits I hope this works for you.

Your method seems plenty fine as well.

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
Kee answered 20/11, 2018 at 6:35 Comment(3)
Thank you, this is great. I accepted the other answer as it came from the package creator and should therefore be considered technically correct.Karolyn
No problem @J.Con. The other answer is more straightforward and from the horse's mouth, as they say. And thanks for the question. I've been looking for a package like this.Kee
This answer helped me a lot. I recommend using the tidyverse package with flextable.Karolyn

© 2022 - 2024 — McMap. All rights reserved.