Closed kmezhoud closed 5 years ago
Hi,
I am not expert with huxtable but it seems there is a difference, flextable starts from a data.frame but huxtable can bind data into the table. I don't think I can translate that directly.
This would be one solution with flextable:
library(flextable)
body <- data.frame( Description = c("Desc1", "Desc2", "Desc3"),
Value = c("vale1", "", "value3"), stringsAsFactors = FALSE)
headr <- data.frame(col_keys = c("Description", "Value"),
line1 = c("subTitle1", ""),
line2 = c("subTitle2", "numeric2"),
line3 = c("Description", "Value"),
stringsAsFactors = FALSE)
your_theme <- function(ft, title){
ft <- add_header_lines(ft, title, top = TRUE)
ft <- flextable::align(ft, part = "all", align = "left")
ft <- flextable::align(ft, i = 1, j = 1, part = "header", align = "center")
ft <- flextable::bold(ft, i = c(1, 4), bold = TRUE, part = "header")
ft <- fontsize(ft, size = 16, part = "all")
ft <- padding(ft, padding = 10, part = "all")
ft <- flextable::width(ft, width = 2)
ft
}
ft <- flextable(data = body)
ft <- set_header_df(x = ft, mapping = headr, key = "col_keys")
ft <- your_theme(ft, title = "Title")
ft <- hline(ft, i = c(1, 3, 4), border = officer::fp_border(), part = "header")
ft
I like set_header_df
as it is easy to define a table and add the content in one single command. If you don't like set_header_df
, another option is to use add_header_row
:
library(flextable)
body <- data.frame( Description = c("Desc1", "Desc2", "Desc3"),
Value = c("vale1", "", "value3"), stringsAsFactors = FALSE)
base_ft <- flextable(data = body, theme_fun = NULL) %>%
add_header_row(values = c("subTitle2", "numeric2"), colwidths = c(1,1), top = TRUE) %>%
add_header_row(values = c("subTitle1"), colwidths = 2, top = TRUE) %>%
add_header_row(values = c("Title"), colwidths = 2, top = TRUE) %>%
fontsize(size = 16, part = "all") %>% padding(padding = 10, part = "all")
base_ft %>%
flextable::align( part = "all", align = "left") %>%
flextable::align( i = 1, j = 1, part = "header", align = "center") %>%
hline(i = c(1, 3, 4), border = officer::fp_border(), part = "header") %>%
flextable::bold(i = c(1, 4), bold = TRUE, part = "header") %>%
flextable::width(width = 2)
You can also use theme functions (see theme_tron_legacy
below) or copy and adapt
from those theme function.
base_ft %>% flextable::theme_tron_legacy() %>%
flextable::width(width = 2) %>%
flextable::align( i = 1, j = 1, part = "header", align = "center")
You can find some documentation here: https://davidgohel.github.io/flextable/articles/layout.html#manage-headers-and-footers
You can get lot of examples and code sample from https://davidgohel.github.io/flextable/articles/offcran/examples.html
@davidgohel Thanks for the quick suggestions.
First point
For the first one, the Description
and value
parameters are defined 3 times.
col_keys
for the headr
despite that it could be in the body
,colnames
of the body
, (which for me the right one),line3
in separate rows.I tried to understand to concept, and I add a column in the body.
col_keys
for the body
and remove of from the headr
... does not workError in fix.by(by.y, y) : 'by' must specify a uniquely valid column
I commented some lines because the number of rows was changed...
library(flextable)
body <- data.frame( col_keys = c("Description", "Value", "Interpretation"),
col1 = c("Desc1", "Desc2", "Desc3"),
col2 = c("value1", "", "value3"),
col3 = c("Inter1", "", "Inter3"),
stringsAsFactors = FALSE)
headr <- data.frame(col_keys = c("foo","too"),
line1 = c("subTitle1", ""),
line2 = c("subTitle2", "numeric2"),
#line3 = c("Description", "Value"),
stringsAsFactors = FALSE)
your_theme <- function(ft, title){
ft <- add_header_lines(ft, title, top = TRUE)
ft <- flextable::align(ft, part = "all", align = "center")
ft <- flextable::align(ft, i = 1, j = 1, part = "header", align = "center")
#ft <- flextable::bold(ft, i = c(1, 4), bold = TRUE, part = "header")
ft <- fontsize(ft, size = 16, part = "all")
ft <- padding(ft, padding = 10, part = "all")
#ft <- flextable::width(ft, width = 2)
ft
}
ft <- flextable(data = body)
ft <- set_header_df(x = ft, mapping = headr, key = "col_keys")
ft <- your_theme(ft, title = "Title")
#ft <- hline(ft, i = c(1, 3, 4), border = officer::fp_border(), part = "header")
ft
Second point
With set_header_df
we can not define the Title
as an argument. We have to add it as a new line separately.
I thing the second suggestion is simplest.
add_header_row
and add_header_lines
? theme_fun
is not known for V0.5.2?Description
and value
are defined as colnames of the body. But during setting layout, they considered as a part of header
with flextable::bold(i = c(1, 4), bold = TRUE, part = "header")
and
hline(i = c(1, 3, 4), border = officer::fp_border(), part = "header")
Thanks, km
I made documentation improvements.
https://davidgohel.github.io/flextable/articles/layout.html#manage-headers-and-footers
It's clearer in the doc
yes, it appeared with 0.5.3
Description
and value
are defined as colnames of the body. But during setting layout, they considered as a part of header withI think the documentation is covering that
KR David
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.
Dear David, Thank you for
flextable
! I would like to defineflextable
as template for reporting. The report will be filled automatically or reactively after processing. Here an example ofhuxtable
. I prefer usingflextable
to define Templates since I have a complex headers. Thanks