flextable
can easily create reporting table from data.frame
. You can merge cells, add header rows, change any format and specify how data should be displayed in cells. flextable
objects can be rendered in HTML format but also in Microsoft Word and PowerPoint documents.
Two functions are provided: flextable
and regulartable
.
flextable
function is producing flexible tables where each cell can contain several chunks of text with their own set of formatting properties (bold, font color, etc.). Function display
lets customise text of cells (See display function).
regulartable
function has been written because the first one is ressource consumming. The only difference is that it is producing tables where cells can contain only one chunk of text with its own set of formatting properties. Function set_formatter
let customise text of cells.
The following table is made with
regulartable
and dataset data:
data <- iris[c(1:3, 51:53, 101:104),]
Let’s have a step by step demo. First create a regulartable and change header font in bold. Function tabwid
will wrap it in an htmlwidget.
library(flextable)
library(officer)
myft <- regulartable(head(mtcars),
col_keys = c("am", "carb", "gear", "mpg", "drat" ))
tabwid(myft)
flextable or regulartable function: regulartable
and flextable
create a flextable object based on input data. Optional argument col_keys
is used to only display a subset of columns.
tabwid function: tabwid
is the function that transform that flextable object into an html widget object (in r markdown documents or shiny applications). We need it here as we are working in an rmarkdown document.
Let’s keep it simple and apply a theme to format the whole table. Functions theme_
are sugar functions whose role is to apply a set of formatting instructions to a flextable. For example, theme_vanilla
set specific borders, right align paragraphs and make headers bold.
myft <- myft %>% theme_vanilla()
tabwid(myft)
To learn more, see article about layouts.
Table layout can be modified. One can add or change header rows, change cells height and width and merge cells. Also, there is an important function named autofit
(it adjusts widths and heights regarding to text widths and heights).
We will use merge_v
to merge identical consecutive cells of columns “carb” and “am”.
myft <- myft %>%
merge_v(j = c("am", "carb") )
tabwid(myft)
Cells can be merged with functions merge_none
, merge_v
and merge_h
.
set_header_labels
set labels:
myft <- myft %>%
set_header_labels( carb = "# carb." ) %>%
width(width = .75) # set width of all columns to .75 in
tabwid(myft)
Headers can be modified with functions set_header_df
, set_header_labels
and add_header
.
autofit
adjust widths and heights of cells. This should be the last operation as some operations make columns wider, e.g. changing font size, changing font weight. autofit
makes sure that any content is displayed as a single line of text.
myft <- myft %>% autofit()
tabwid(myft)
To learn more, see article about format.
Many sugar functions can be used to format flextables: bg
, fontsize
, italic
, bold
, color
, padding
, border
.
myft <- myft %>% italic(j = 1) %>%
bg(bg = "#C90000", part = "header") %>%
color(color = "white", part = "header") %>%
border(border = fp_border(color = "orange"), part = "all")
tabwid(myft)
Conditional formatting can be made by using the selector arguments. All formatting functions are accepting selector arguments.
myft <- myft %>%
color(~ drat > 3.5, ~ drat, color = "red") %>%
bold(~ drat > 3.5, ~ drat, bold = TRUE) %>%
autofit()
tabwid(myft)
Flextables can be inserted in r markdown documents and in shiny applications; in these cases, use tabwid
. When working in RStudio, flextable will be printed in the rstudio viewer pane and call to tabwid
is not necessary. Note that flextables are not designed to work with flexdashboard documents.
To add these objects in PowerPoint or Word documents, use functions: - ph_with_flextable
or ph_with_flextable_at
(PowerPoint) - body_add_flextable
(Word)
officer
package is required to create a PowerPoint or Word document.
library(officer)
The following is producing a PowerPoint document:
ft <- regulartable(head(mtcars)) %>%
theme_booktabs() %>%
autofit()
ppt <- read_pptx() %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with_flextable(value = ft, type = "body")
print(ppt, target = "assets/pptx/example.pptx") %>% invisible()
Download file example.pptx - view with office web viewer
The following is producing a Word document:
doc <- read_docx() %>%
body_add_flextable(value = ft)
print(doc, target = "assets/docx/example.docx") %>% invisible()
Download file example.docx - view with office web viewer