Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

knit_print.flextable

Render flextable in rmarkdown


Description

Function used to render flextable in knitr/rmarkdown documents.

You should not call this method directly. This function is used by the knitr package to automatically display a flextable in an "R Markdown" document from a chunk. However, it is recommended to read its documentation in order to get familiar with the different options available.

HTML, Word, PowerPoint and PDF outputs are supported.

Table captioning is a flextable feature compatible with R Markdown documents. The feature is available for HTML, PDF and Word documents. Compatibility with the "bookdown" package is also ensured, including the ability to produce captions so that they can be used in cross-referencing.

Usage

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

Arguments

x

a flextable object

...

further arguments, not used.

Chunk options

Some features, often specific to an output format, are available to help you configure some global settings relatve to the table output. knitr's chunk options are to be used to change the default settings:

chunk option property default value HTML docx PDF pptx
ft.align flextable alignment, supported values are 'left', 'center' and 'right' 'center' yes yes yes no
ft.shadow HTML option, disable shadow dom (set to FALSE) for pagedown. TRUE yes no no no
ft.split Word option 'Allow row to break across pages' can be activated when TRUE. FALSE no yes no no
ft.tabcolsep space between the text and the left/right border of its containing cell 8.0 no no yes no
ft.arraystretch height of each row relative to its default height 1.5 no no yes no
ft.left left coordinates in inches 1.0 no no no yes
ft.top top coordinates in inches 2.0 no no no yes

Table caption

Captions can be defined in two ways.

The first is with the set_caption function. If it is used, the other method will be ignored. The second method is by using knitr chunk option tab.cap.

set_caption(x, caption = "my caption")

If set_caption function is not used, caption identifier will be read from knitr's chunk option tab.id or label if in a bookdown (this is to respect the bookdown standards).

tab.id='my_id' or label='my_id'.

Some options are available to customise captions for any output:

label name value
caption id/bookmark tab.id NULL
caption tab.cap NULL
display table caption on top of the table or not tab.topcaption TRUE
caption table sequence identifier. tab.lp "tab:"

Word output provide more options such as ability to choose the prefix for numbering chunk for example. The table below expose these options:

label name value
Word stylename to use for table captions. tab.cap.style NULL
prefix for numbering chunk (default to "Table "). tab.cap.pre Table
suffix for numbering chunk (default to ": "). tab.cap.sep " :"
title number depth tab.cap.tnd 0
caption prefix formatting properties tab.cap.fp_text fp_text_lite(bold = TRUE)
separator to use between title number and table number. tab.cap.tns "-"

HTML output

HTML output is using shadow dom to encapsule the table into an isolated part of the page so that no clash happens with styles. Some output may not support this feature. To our knowledge, only the pagedown output is concerned. Use knitr chunk option ft.shadow=FALSE to disable shadow dom.

If ft.shadow=TRUE some global CSS rules may change the desired output of flextables.

PDF output

Some features are not implemented in PDF due to technical infeasibility. These are the padding, line_spacing and height properties.

Background color and merged cells are also sources of trouble with PDF format. Authors are hoping to fix these issues in the future.

PowerPoint output

Auto-adjust Layout is not available for PowerPoint.

Images cannot be integrated into tables with the PowerPoint format.

Note

Supported formats require some minimum pandoc versions:

Output format pandoc minimal version
HTML >= 1.12
Word (docx) >= 2.0
PowerPoint (pptx) >= 2.4
PDF >= 1.12

See Also

Examples

# simple examples -----
demo_docx <- system.file(package = "flextable", "examples/rmd", "demo.Rmd")
rmd_file <- tempfile(fileext = ".Rmd")
file.copy(demo_docx, to = rmd_file, overwrite = TRUE)
rmd_file # R Markdown document used for demo
if(require("rmarkdown", quietly = TRUE)){
#  knitr::opts_chunk$set(webshot = "webshot2")
#  render(input = rmd_file, output_format = "word_document", output_file = "doc.docx")
#  render(input = rmd_file, output_format = "pdf_document", output_file = "doc.pdf")
#  render(input = rmd_file, output_format = "html_document", output_file = "doc.html")
#  render(input = rmd_file, output_format = "powerpoint_presentation", output_file = "pres.pptx")
#  render(input = rmd_file, output_format = "slidy_presentation", output_file = "slidy.html")
#  render(input = rmd_file, output_format = "beamer_presentation", output_file = "beamer.pdf")
#  render(input = rmd_file, output_format = "pagedown::html_paged", output_file = "paged.html")
}


## bookdown examples wth captions and cross ref -----
# captions_example <- system.file(
#   package = "flextable",
#   "examples/rmd", "captions_example.Rmd")
#
# dir_tmp <- tempfile(pattern = "dir")
# dir.create(dir_tmp, showWarnings = FALSE, recursive = TRUE)
# file.copy(captions_example, dir_tmp)
# rmd_file <- file.path(dir_tmp, basename(captions_example))
#
# file.copy(captions_example, to = rmd_file, overwrite = TRUE)
#
# if(require("rmarkdown", quietly = TRUE)){
#   render(input = rmd_file,
#          output_format = word_document(),
#          output_file = "doc.docx")
#   render(input = rmd_file,
#          output_format = pdf_document(latex_engine = "xelatex"),
#          output_file = "doc.pdf")
#   render(input = rmd_file,
#          output_format = html_document(),
#          output_file = "doc.html")
#
#   # bookdown ----
#   if(require("bookdown", quietly = TRUE)){
#     render(input = rmd_file, output_format = word_document2(),
#            output_file = "book.docx")
#     render(input = rmd_file,
#            output_format = pdf_document2(latex_engine = "xelatex"),
#            output_file = "book.pdf")
#     render(input = rmd_file,
#            output_format = html_document2(),
#            output_file = "book.html")
#
#     # officedown ----
#     if(require("officedown", quietly = TRUE)){
#       render(input = rmd_file,
#              output_format = markdown_document2(base_format=rdocx_document),
#              output_file = "officedown.docx")
#     }
#   }
# }
# browseURL(dirname(rmd_file))

flextable

Functions for Tabular Reporting

v0.6.10
GPL-3
Authors
David Gohel [aut, cre], Clementine Jager [ctb], Quentin Fazilleau [ctb], Maxim Nazarov [ctb] (rmarkdown for docx output), Titouan Robert [ctb], Michael Barrowman [ctb] (inline footnotes), Atsushi Yasumoto [ctb] (support for bookdown cross reference), Paul Julian [ctb] (support for gam objects)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.