Search code examples
rheaderflextable

flextable: systematically change multiple headers' labels


I'm looking to change the S_1 through S_4 here with a one-liner, instead of typing them all out in set_header_label.

enter image description here`

how I made up ft1

ft1 <- head(iris) %>%
  flextable() %>%
set_header_labels(
  values = list(
    Sepal.Length = "S_1",
    Sepal.Width = "S_2",
    Petal.Length = "S_3",
    Petal.Width = "S_4"
  )
)

The rough idea of the one-liner

ft1 %>%
  set_header_labels(
  values = list(list(S_1:S_4) = list(paste("\U03A3", c(1:4), sep = "_"))))

Solution

  • I think one approach is to create a named list separately, and then use in set_header_labels. Let me know if this is what you were looking for.

    library(dplyr)
    library(flextable)
    
    my_list <- setNames(as.list(paste0("S_", 1:4)), names(iris[1:4]))
    
    ft1 <- head(iris) %>%
      flextable() %>%
      set_header_labels(values = my_list)