Learn R Programming

flextable R package

The flextable package provides a framework for easily create tables for reporting and publications. Tables can be easily formatted with a set of verbs such as bold(), color(), they can receive a header of more than one line, cells can be merged or contain an image. The package make it possible to build any table for publication from a data.frame and provides sugar function as_flextable() to convert several R objects to a flextable, such as an object return from table() or a model.

Tables can be embedded within HTML, PDF, Word and PowerPoint documents from R Markdown documents and within RTF or Microsoft Word or PowerPoint documents with package officer. Tables can also be rendered as R plots or graphic files (png).

flextable(mtcars) %>% 
  theme_vanilla() %>% 
  save_as_docx(path = "mytable.docx")

A flextable object is a data.frame representation. An API is available to let R users create tables for reporting and control their formatting properties and their layout. The package provides functions that give control over:

  • header, body and footer content
  • text, paragraphs, cells and border formatting of any element
  • displayed values

The package also offers a set of high-level functions that allow tabular reporting of statistical models and the creation of complex cross tabulations.

Examples

library(flextable)
set_flextable_defaults(
  font.family = "Arial", font.size = 10, 
  border.color = "gray", big.mark = "")

ft <- flextable(head(mtcars)) |> 
  bold(part = "header") 
ft

ft |> 
  highlight(i = ~ mpg < 22, j = "disp", color = "#ffe842") |> 
  bg(j = c("hp", "drat", "wt"), 
     bg = scales::col_quantile(palette = c("wheat", "red"), domain =NULL)) |> 
  add_footer_lines("The 'mtcars' dataset")
ggplot2::diamonds[, c("cut", "carat", "price", "clarity", "table")] |> 
  summarizor(by = c("cut")) |> 
  as_flextable(spread_first_col = TRUE)

Installation

install.packages("flextable")

You can get the development version from GitHub:

devtools::install_github("davidgohel/flextable")

Resources

Getting help / questions

If you have questions about how to use the package, visit Stackoverflow and use tags flextable and r Stackoverflow link. You can also use https://github.com/davidgohel/flextable/discussions to start a discussion.

Bug reports

When you file a bug report, please spend some time making it easy for us to reproduce. If you take the time to make the bug report consistent, it will be easier to fix.

Copy Link

Version

Install

install.packages('flextable')

Monthly Downloads

74,548

Version

0.9.5

License

GPL-3

Maintainer

David Gohel

Last Published

March 6th, 2024

Functions in flextable (0.9.5)

align

Set text alignment
add_footer_row

Add footer labels
as_flextable.glm

Transform a 'glm' object into a flextable
as_flextable.grouped_data

Transform a 'grouped_data' object into a flextable
as_flextable.pam

Transform a 'pam' object into a flextable
add_footer

Add column values as new lines in footer
add_body_row

Add body labels
as_bracket

Chunk with values in brackets
add_header_lines

Add labels as new rows in the header
as_flextable.summarizor

Transform a 'summarizor' object into a flextable
as_chunk

Chunk of text wrapper
add_footer_lines

Add labels as new rows in the footer
as_i

Italic chunk
as_image

Image chunk wrapper
as_equation

Equation chunk
add_header_row

Add header labels
as_flextable.lm

Transform a 'lm' object into a flextable
as_flextable.merMod

Transform a mixed model into a flextable
as_sub

Subscript chunk
as_flextable.data.frame

Transform and summarise a 'data.frame' into a flextable Simple summary of a data.frame as a flextable
add_header

Add column values as new lines in header
as_flextable

Method to transform objects into flextables
as_flextable.gam

Transform a 'gam' model into a flextable
as_flextable.tabulator

Transform a 'tabulator' object into a flextable
bold

Set bold font
as_flextable.xtable

Transform a 'xtable' object into a flextable
as_grouped_data

Add row separators to grouped data
as_sup

Superscript chunk
append_chunks

Append chunks to flextable content
border_inner

Set vertical & horizontal inner borders
border

Cell borders update
colformat_lgl

Format logical cells
border_inner_h

Set inner borders
colformat_num

Format numeric cells
as_b

Bold chunk
as_flextable.htest

Transform a 'htest' object into a flextable
as_flextable.table

Transform a 'table' object into a flextable
color

Set font color
as_highlight

Highlight chunk
before

Is an element before a match with entries
as_flextable.kmeans

Transform a 'kmeans' object into a flextable
bg

Set background color
border_remove

Remove borders
as_word_field

'Word' computed field
colorize

Colorize chunk
autofit

Adjusts cell widths and heights
chunk_dataframe

Create a chunk representation suitable for flextable
dim.flextable

Get widths and heights of flextable
as_flextable.tabular

Transform a 'tables::tabular' object into a flextable
dim.flextableGrob

Get optimal width and height of a flextable grob
flextable_to_rmd

flextable raw code
colformat_datetime

Format datetime cells
fit_to_width

Fit a flextable to a maximum width
fmt_2stats

Format content for data generated with summarizor()
fmt_n_percent

Format content for count data
colformat_double

Format numeric cells
compose

Define displayed values and mixed content
border_inner_v

Set vertical inner borders
border_outer

Set outer borders
as_paragraph

Concatenate chunks in a flextable
fix_border_issues

Fix border issues when cell are merged
fmt_header_n

Format count data for headers
fmt_pct

Format numerical data as percentages
colformat_image

Format cells as images
continuous_summary

Continuous columns summary
fmt_int

Format numerical data as integer
colformat_int

Format integer cells
dim_pretty

Calculate pretty dimensions
get_flextable_defaults

Get flextable defaults formatting properties
empty_blanks

Make blank columns as transparent
delete_columns

Delete flextable columns
gg_chunk

'ggplots' chunk wrapper
delete_part

Delete flextable part
grid_chunk

'Grid Graphics' chunk wrapper
headers_flextable_at_bkm

Add flextable at a bookmark location in document's header
information_data_paragraph

paragraph related information of a flextable
fontsize

Set font size
hline

Set horizontal borders
as_raster

Transform a flextable into a raster
italic

Set italic font
labelizor

Change displayed labels
hline_bottom

Set bottom horizontal border
flextable-package

flextable: Functions for Tabular Reporting
flextable

flextable creation
body_add_flextable

Add flextable into a Word document
line_spacing

Set text alignment
linerange

Mini linerange chunk wrapper
fmt_signif_after_zeros

Format with significant figures after zeros
merge_v

Merge flextable cells vertically
minibar

Mini barplots chunk wrapper
save_as_image

Save a flextable in a 'png' or 'svg' file
body_replace_flextable_at_bkm

Add flextable at boorkmark location in a Word document
lollipop

Mini lollipop chart chunk wrapper
save_as_pptx

Save flextable objects in a 'PowerPoint' file
font

Set font
fp_text_default

Text formatting properties
colformat_char

Format character cells
footers_flextable_at_bkm

Add flextable at a bookmark location in document's footer
footnote

Add footnotes to flextable
ph_with.flextable

Add a flextable into a PowerPoint slide
save_as_rtf

Save flextable objects in an 'RTF' file
colformat_date

Format date cells
delete_rows

Delete flextable rows
fp_border_default

Border formatting properties
hline_top

Set top horizontal border
gen_grob

Convert a flextable to a grid grob object
hrule

Set flextable rule for rows heights
separate_header

Split column names using a separator into multiple rows
df_printer

data.frame automatic printing as a flextable
tabulator_colnames

Column keys of tabulator objects
theme_alafoli

Apply alafoli theme
plot.flextable

Plot a flextable
htmltools_value

flextable as an 'HTML' object
save_as_docx

Save flextable objects in a 'Word' file
save_as_html

Save flextable objects in an 'HTML' file
set_formatter

Set column formatter functions
hyperlink_text

Chunk of text with hyperlink
merge_h_range

Rowwise merge of a range of columns
merge_at

Merge flextable cells into a single one
merge_h

Merge flextable cells horizontally
set_header_footer_df

Set flextable's header or footer rows
ncol_keys

Number of columns
nrow_part

Number of rows of a part
merge_none

Delete flextable merging informations
padding

Set paragraph paddings
theme_tron_legacy

Apply tron legacy theme
theme_vader

Apply Sith Lord Darth Vader theme
paginate

Paginate tables
flextable_dim

Get width and height of a flextable object
flextable_html_dependency

htmlDependency for flextable objects
prepend_chunks

Prepend chunks to flextable content
print.flextable

flextable printing
fmt_avg_dev

Format content for mean and sd
set_caption

Set Caption
vline_right

Set flextable right vertical borders
void

Delete flextable content
proc_freq

Frequency table
regulartable

flextable old functions
fmt_dbl

Format numerical data
summarizor

Data summary preparation
set_header_labels

Change headers labels
surround

Set borders for a selection of cells
set_flextable_defaults

Modify flextable defaults formatting properties
height

Set flextable rows height
theme_vanilla

Apply vanilla theme
highlight

Text highlight color
set_table_properties

Global table properties
theme_apa

Apply APA theme
theme_booktabs

Apply booktabs theme
use_model_printer

set model automatic printing as a flextable
theme_zebra

Apply zebra theme
information_data_cell

table cell related information of a flextable
theme_box

Apply box theme
information_data_chunk

content chunk related information of a flextable
valign

Set vertical alignment
width

Set columns width
theme_tron

Apply tron theme
keep_with_next

Set Word 'Keep with next' instructions
vline_left

Set flextable left vertical borders
vline

Set vertical borders
knit_print.flextable

Render flextable with 'knitr'
plot.flextableGrob

plot a flextable grob
plot_chunk

Mini plots chunk wrapper
rotate

Rotate cell text
rtf_add.flextable

Add a 'flextable' into an RTF document
shift_table

Create a shift table
style

Set flextable style
tab_settings

Set tabulation marks configuration
tabulator

Tabulation of aggregations
to_html.flextable

Get HTML code as a string
use_df_printer

Set data.frame automatic printing as a flextable
add_body

Add column values as new lines in body
add_latex_dep

Add latex dependencies