knit_print.flextable(), which is called automatically when a flextable object appears in a code chunk. You do not need to call it directly.
How rendering works
When knitr processes a chunk containing a flextable, it detects the output format and generates the appropriate output:| Output format | Rendering method |
|---|---|
| HTML | Native HTML table with scoped CSS |
| Word (docx) | Office Open XML (wml) |
| LaTeX table code | |
| PowerPoint (pptx) | Presentation ML (pml) |
| Other (GitHub, Beamer, etc.) | PNG image via save_as_image() |
Adding captions
The recommended way to add a caption is withset_caption():
set_caption() is used, chunk options related to captions are ignored.
In Quarto, use tbl-cap and label chunk options instead of set_caption():
@tbl-airquality. In bookdown documents, cross-references use \@ref(tab:chunk_label).
Chunk options reference
All formats
| Option | Description | Default |
|---|---|---|
ft.align | Table alignment: 'left', 'center', 'right' | 'center' |
R Markdown captions
These options are only used whenset_caption() has not been called on the table:
| Option | Description | Default |
|---|---|---|
tab.cap | Caption text | NULL |
tab.id | Caption id / bookmark | NULL |
tab.topcaption | Place caption above the table | TRUE |
tab.lp | Caption sequence identifier | "tab:" |
tab.cap.style | Word paragraph style for the caption | NULL |
HTML
| Option | Description | Default |
|---|---|---|
ft.htmlscroll | Enable horizontal scrolling | FALSE |
Word
| Option | Description | Default |
|---|---|---|
ft.split | Allow rows to break across pages | TRUE |
| Option | Description | Default |
|---|---|---|
ft.tabcolsep | Space between text and cell borders (pt) | 0 |
ft.arraystretch | Row height multiplier | 1.5 |
ft.latex.float | Float placement: 'none', 'float', 'wrap-r', 'wrap-l', 'wrap-i', 'wrap-o' | 'none' |
PowerPoint
| Option | Description | Default |
|---|---|---|
ft.left | Left position of table placeholder (inches) | 1 |
ft.top | Top position of table placeholder (inches) | 2 |
knitr::opts_chunk$set():
officedown caption options
When usingofficedown::rdocx_document(), additional caption chunk options become available:
| Option | Description | Default |
|---|---|---|
tab.cap.pre | Numbering prefix | "Table " |
tab.cap.sep | Numbering suffix | ": " |
tab.cap.tnd | Title number depth | 0 |
tab.cap.fp_text | Caption prefix formatting | fp_text_lite(bold=TRUE) |
tab.cap.tns | Title / table number separator | "-" |
Tables inside loops
To print flextable objects insidefor loops or if statements, use flextable_to_rmd(). Set the chunk option results = 'asis':
Automatic data frame conversion
use_df_printer() replaces the default data frame print method so that any data frame printed in a document is automatically converted to a flextable:
use_model_printer() to do the same for model objects. These functions are best placed in a setup chunk.
Quarto setup with use_flextable_qmd()
The flextable-qmd Lua filter extension allows Quarto markdown syntax inside flextable cells: cross-references, bold, italic, links, math, inline code, and shortcodes. Install it with:
as_qmd() to mark cell content as Quarto markdown:
Post-processing hooks
set_flextable_defaults() accepts post-processing functions that are applied to every table before rendering. This is useful for applying consistent styling across a document:
post_process_all, post_process_html, post_process_docx, post_process_pdf, post_process_pptx.