knit_print.flextable: Render flextable in knitr documents

View source: R/printers.R

knit_print.flextableR Documentation

Render flextable in knitr documents

Description

This function is called automatically by knitr to display a flextable in R Markdown and Quarto documents. You do not need to call it directly.

Supported output formats: HTML, Word (docx), PDF and PowerPoint (pptx). For other formats (e.g., github_document, beamer_presentation), the table is rendered as a PNG image.

Usage

## S3 method for class 'flextable'
knit_print(x, ...)

Arguments

x

a 'flextable' object, see flextable-package to learn how to create 'flextable' object.

...

unused.

Getting started

No special setup is needed: place a flextable object in a code chunk and it will be rendered in the output document.

Add a caption with set_caption():

ft <- set_caption(ft, caption = "My table caption")

In Quarto documents, use chunk options tbl-cap and label instead:

```{r}
#| label: tbl-mytable
#| tbl-cap: "My table caption"
ft
```

Captions

Recommended method: use set_caption() to define the caption directly on the flextable object. When set_caption() is used, chunk options related to captions are ignored.

Alternative (R Markdown only): use knitr chunk options tab.cap and tab.id:

Description Chunk option Default
Caption text tab.cap NULL
Caption id/bookmark tab.id NULL
Caption on top of the table tab.topcaption TRUE
Caption sequence identifier tab.lp "tab:"
Word style for captions tab.cap.style NULL

Bookdown: cross-references use the pattern ⁠\@ref(tab:chunk_label)⁠. The usual bookdown numbering applies.

Quarto: cross-references use ⁠@tbl-chunk_label⁠. To embed cross-references or other Quarto markdown inside flextable cells, use as_qmd() with the flextable-qmd extension (see use_flextable_qmd()).

Chunk options

Use knitr::opts_chunk$set(...) to set defaults for the whole document.

All formats:

  • ft.align: table alignment, one of 'left', 'center' (default) or 'right'.

HTML:

  • ft.htmlscroll: TRUE or FALSE (default) to enable horizontal scrolling. See set_table_properties() for finer control.

Word:

  • ft.split: allow rows to break across pages (TRUE by default).

PDF:

  • ft.tabcolsep: space between text and cell borders (default 0 pt).

  • ft.arraystretch: row height multiplier (default 1.5).

  • ft.latex.float: floating placement. One of 'none' (default), 'float', 'wrap-r', 'wrap-l', 'wrap-i', 'wrap-o'.

PowerPoint:

  • ft.left, ft.top: top-left coordinates of the table placeholder in inches (defaults: 1 and 2).

Word with officedown

When using officedown::rdocx_document(), additional caption options are available:

Description Chunk option Default
Numbering prefix tab.cap.pre "Table "
Numbering suffix tab.cap.sep ": "
Title number depth tab.cap.tnd 0
Caption prefix formatting tab.cap.fp_text fp_text_lite(bold=TRUE)
Title number / table number separator tab.cap.tns "-"

Quarto

flextable works natively in Quarto documents for HTML, PDF and Word.

The flextable-qmd Lua filter extension enables Quarto markdown inside flextable cells: cross-references (⁠@tbl-xxx⁠, ⁠@fig-xxx⁠), bold/italic, links, math, inline code and shortcodes. See as_qmd() and use_flextable_qmd() for setup instructions.

PDF accessibility (PDF/UA-2)

Quarto's pdf-standard: ua-2 injects ⁠\DocumentMetadata{tagging=on}⁠ in the LaTeX preamble, activating 'tagpdf'. This code patches LaTeX commands at compile time to insert PDF structure tags. Neither Quarto nor flextable control this process.

The tagging code does not yet support longtable + colortbl, booktabs rules, and cline, which flextable relies on. Compilation fails with ⁠\ERRORtbl/row⁠. When these upstream issues are resolved, flextable PDF output will support tagging without changes. Other formats are not affected.

PDF limitations

The following properties are not supported in PDF output: padding.top, padding.bottom, line_spacing and row height. Justified text is converted to left-aligned.

To use system fonts, set latex_engine: xelatex in the YAML header (the default pdflatex engine does not support them).

See add_latex_dep() when caching flextable results.

PowerPoint limitations

PowerPoint only supports fixed table layout. Use autofit() to adjust column widths. Images inside table cells are not supported (this is a PowerPoint limitation).

HTML note

HTML output uses Shadow DOM to isolate table styles from the rest of the page.

See Also

set_caption(), as_qmd(), use_flextable_qmd(), paginate()

Other functions for flextable output and export: df_printer(), flextable_to_rmd(), gen_grob(), htmltools_value(), plot.flextable(), print.flextable(), save_as_docx(), save_as_html(), save_as_image(), save_as_pptx(), save_as_rtf(), to_html.flextable(), wrap_flextable()

Examples

## Not run: 
library(rmarkdown)
if (pandoc_available() &&
  pandoc_version() > numeric_version("2")) {
  demo_loop <- system.file(
    package = "flextable",
    "examples/rmd",
    "demo.Rmd"
  )
  rmd_file <- tempfile(fileext = ".Rmd")
  file.copy(demo_loop, to = rmd_file, overwrite = TRUE)
  render(
    input = rmd_file, output_format = "html_document",
    output_file = "demo.html"
  )
}

## End(Not run)

flextable documentation built on June 2, 2026, 9:08 a.m.