flextable (version 0.5.11)

knit_print.flextable: Render flextable in rmarkdown

Description

Function used to render flextable in knitr/rmarkdown documents. HTML, Word and PowerPoint outputs are supported.

Usage

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

Arguments

x

a flextable object

...

further arguments, not used.

HTML chunk options

Result can be aligned with chunk option ft.align that accepts values 'left', 'center' and 'right'.

Word chunk options

Result can be aligned with chunk option ft.align that accepts values 'left', 'center' and 'right'.

Word option 'Allow row to break across pages' can be activated with chunk option ft.split set to TRUE.

Table captioning is a flextable feature compatible with knitr. Three methods are available and are presented below in order of triggering:

  • with the set_caption function, if the function is used, this definition will be chosen.

  • with knitr's chunk options:

    • tab.cap.style: Word style name to use for table captions.

    • tab.cap.pre: Prefix for numbering chunk (default to "Table").

    • tab.cap.sep: Suffix for numbering chunk (default to ": ").

    • tab.cap: Caption label.

    • tab.id: Caption reference unique identifier.

  • with knitr chunk and bookdown options (if you're in a bookdown):

    • tab.cap.style: Word style name to use for table captions.

    • tab.cap.pre: Prefix for numbering chunk (default to "Table").

    • tab.cap.sep: Suffix for numbering chunk (default to ": ").

    • tab.cap: Caption label.

    • label: Caption reference unique identifier.

PowerPoint chunk options

Position should be defined with options ft.left and ft.top. Theses are the top left coordinates of the placeholder that will contain the table. They default to {r ft.left=1, ft.top=2}.

PDF chunk options

Using flextable with template pdf_document is OK if the flextable fits on one single page. The PDF output is not a real latex output but a PNG image generated with package 'webshot' or package 'webshot2'. Package 'webshot2' should be prefered as 'webshot' can have issues with some properties (i.e. bold are not rendered for some users).

To specify usage of 'webshot2', use chunk option webshot="webshot2".

See Also

Other flextable print function: as_raster(), docx_value(), htmltools_value(), plot.flextable(), print.flextable(), save_as_docx(), save_as_html(), save_as_image(), save_as_pptx()

Examples

Run this code
# NOT RUN {
# 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")
}


# looping examples for Word output -----
demo_loop <- system.file(package = "flextable", "examples/rmd", "loop_docx.Rmd")
rmd_file <- tempfile(fileext = ".Rmd")
file.copy(demo_loop, to = rmd_file, overwrite = TRUE)
rmd_file # R Markdown document used for demo
if(require("rmarkdown", quietly = TRUE)){
#  render(input = rmd_file, output_format = "word_document", output_file = "loop_docx.docx")
}

# looping examples for HTML output -----
demo_loop <- system.file(package = "flextable", "examples/rmd", "loop_html.Rmd")
rmd_file <- tempfile(fileext = ".Rmd")
file.copy(demo_loop, to = rmd_file, overwrite = TRUE)
rmd_file # R Markdown document used for demo
if(require("rmarkdown", quietly = TRUE)){
#  render(input = rmd_file, output_format = "html_document", output_file = "loop_html.html")
}
# }

Run the code above in your browser using DataLab