davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
561 stars 81 forks source link

set_header_label() not possible after separate_header() #593

Closed namarkus closed 11 months ago

namarkus commented 11 months ago

Hi there, thank for this greate package! The new separate_header() function is very practical. Unfortunately, it affects the operation of the set_header_label() function.

For layout reasons, I would like to remove the heading of a column. However, this is no longer possible after the separate_header() function:

ft <- flextable(head(iris))
ft <- separate_header(ft, split = '.', fix = TRUE)
ft <- set_header_labels(ft, Species = "")

The label "Species" remains.

If I modify the label first, it works so far:

ft <- flextable(head(iris))
ft <- set_header_labels(ft, Species = "")

The labels "Species" is gone. But after separating header the label "Species" is set again:

ft <- separate_header(ft, split = '.', fix = TRUE)

I can modify header labels that have been separated:

ft <- flextable(head(iris))
ft <- separate_header(ft, split = '.', fix = TRUE)
ft <- set_header_labels(ft, Sepal.Length = "Longeur")

But not the ones that don't have been separated (like "Species" in this example. I guess set_header_label() should set header labels regardless of whether they have been split or not.

davidgohel commented 11 months ago

Hello,

Not really. The separate_header() function will create columns of values starting with the topmost row. Here, "Species" is added to the first row (and last column), then a blank is added.

The set_header_labels() function will replace the corresponding value in the last row of the header. However, separate_header() has already merged the first and last lines of the last column, and in this case, it's the first value (i.e. first line) that is displayed, while the last line is not.

You can check this by using merge_none():

ft <- flextable(head(iris))
ft <- separate_header(ft, split = '.', fix = TRUE)
ft <- set_header_labels(ft, Species = "zzz")
ft <- merge_none(ft)
ft
Capture d’écran 2023-12-10 à 20 45 30

In your case, you can use labelizor() instead, it is very handy to raw-replace some labels and quite simple:

ft <- flextable(head(iris))
ft <- separate_header(ft, split = '.', fix = TRUE)
ft <- labelizor(ft, labels = c("Species" = "zzz"), part = "header")
ft
Capture d’écran 2023-12-10 à 20 47 45