flextable provides two approaches for Word and PowerPoint output: convenience functions that create a new file directly, and officer integration functions for adding tables to existing documents.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/davidgohel/flextable/llms.txt
Use this file to discover all available pages before exploring further.
Word output
Save to a new Word file
save_as_docx() creates a Word document containing one or more tables. Each table is placed on its own section separated by a blank paragraph. If the objects are named, the names are used as heading 2 titles above each table.
Page layout with pr_section
Pass an officer::prop_section object to control page orientation, size, and margins:
RTF output
save_as_rtf() works identically to save_as_docx() but produces an RTF file. It also accepts a pr_section argument and supports page headers:
Adding to an existing officer document
body_add_flextable()
Use body_add_flextable() to add a table to an rdocx object created with officer::read_docx(). This gives you full control over document construction:
| Parameter | Description | Default |
|---|---|---|
x | An rdocx object | |
value | A flextable object | |
align | "left", "center", or "right" | NULL (inherits from table) |
pos | Cursor position: "after", "before", "on" | "after" |
split | Allow row breaks across pages | NULL |
topcaption | Place caption above (TRUE) or below (FALSE) the table | TRUE |
Prefer
set_flextable_defaults(table_align = "center") and set_table_properties(opts_word = list(split = TRUE)) over the align and split arguments in body_add_flextable().Replace content at a bookmark
body_replace_flextable_at_bkm() replaces a bookmarked paragraph in a Word document with a flextable. The bookmark is removed as a side effect:
PowerPoint output
Save to a new PowerPoint file
save_as_pptx() creates a PowerPoint presentation with one table per slide. Named objects become slide titles:
PowerPoint ignores captions set with
set_caption(). Captions are not supported in the PowerPoint format.Add to an existing officer presentation
ph_with() is the officer generic for adding content to a placeholder. flextable provides the ph_with.flextable() method, which converts the table to a native PowerPoint table:
officer::ph_location_type() to target a specific placeholder type:
Table width and height are determined by flextable cell dimensions, not by the placeholder location. Use
width(), height(), autofit(), and dim_pretty() to control the table size before placing it.PowerPoint limitations
- PowerPoint only supports fixed table layout. Use
autofit()to adjust column widths. - Images inside table cells are not supported in PowerPoint.
- Row and column spans rendered from flextable may differ from native PowerPoint span behavior.
Pagination and page breaks
paginate() controls how table rows behave across page breaks in Word and RTF output. It applies the “keep with next” attribute to groups of rows so they stay together on a page.
group_def argument controls how groups are identified:
| Value | Algorithm |
|---|---|
"rle" | Groups consecutive equal values in group column (use with tabulator()) |
"nonempty" | A new group starts wherever group column is non-empty |
"starts" | group is an integer vector of row indices that begin new groups |
paginate() only applies to Word and RTF output. It has no effect on HTML or PDF.Clinical shift tables
shift_table() prepares a laboratory test results dataset in CDISC format for display as a shift table with tabulator(). A shift table shows how subjects move between reference range categories (Low/Normal/High) from baseline to follow-up visits.
shift_table() returns the processed data frame with three attributes: VISIT_N (visit counts for use as hidden_data), FUN_VISIT (factor conversion for visit column), and FUN_GRADE (factor conversion adding MISSING/SUM levels).