Create a summary from a data.frame as a flextable. This function is to be used in an R Markdown document.

To use that function, you must declare it in the part df_print of the 'YAML' header of your R Markdown document:

---
df_print: !expr function(x) flextable::df_printer(x)
---

We notice an unexpected behavior with bookdown. When using bookdown it is necessary to use use_df_printer() instead in a setup run chunk:

use_df_printer()

df_printer(dat, ...)

Arguments

dat

the data.frame

...

unused argument

Details

'knitr' chunk options are available to customize the output:

  • ft_max_row: The number of rows to print. Default to 10.

  • ft_split_colnames: Should the column names be split (with non alpha-numeric characters). Default to FALSE.

  • ft_short_strings: Should the character column be shorten. Default to FALSE.

  • ft_short_size: Maximum length of character column if ft_short_strings is TRUE. Default to 35.

  • ft_short_suffix: Suffix to add when character values are shorten. Default to "...".

  • ft_do_autofit: Use autofit() before rendering the table. Default to TRUE.

  • ft_show_coltype: Show column types. Default to TRUE.

  • ft_color_coltype: Color to use for column types. Default to "#999999".

Examples

df_printer(head(mtcars))
#> [1] "```{=html}\n<div class=\"tabwid\"><style>.cl-df980cfe{}.cl-df92142a{font-family:'DejaVu Sans';font-size:11pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-df92143e{font-family:'DejaVu Sans';font-size:11pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(153, 153, 153, 1.00);background-color:transparent;}.cl-df922488{margin:0;text-align:right;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-df926632{width:65.6pt;background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-df92663c{width:65.6pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-df926646{width:65.6pt;background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(102, 102, 102, 1.00);border-top: 2pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-df926647{width:65.6pt;background-color:transparent;vertical-align: middle;border-bottom: 2pt solid rgba(102, 102, 102, 1.00);border-top: 1pt solid rgba(102, 102, 102, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table class='cl-df980cfe'>\n```\n\n```{=html}\n<thead><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">mpg</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">cyl</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">disp</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">hp</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">drat</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">wt</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">qsec</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">vs</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">am</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">gear</span></p></td><td class=\"cl-df926646\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">carb</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td><td class=\"cl-df926647\"><p class=\"cl-df922488\"><span class=\"cl-df92143e\">numeric</span></p></td></tr></thead><tbody><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">21.0</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">6</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">160</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">110</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3.9</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">2.6</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">16.5</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">0</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">1</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">4</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">4</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">21.0</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">6</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">160</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">110</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3.9</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">2.9</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">17.0</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">0</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">1</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">4</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">4</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">22.8</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">4</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">108</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">93</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3.9</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">2.3</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">18.6</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">1</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">1</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">4</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">1</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">21.4</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">6</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">258</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">110</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3.1</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3.2</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">19.4</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">1</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">0</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">1</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">18.7</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">8</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">360</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">175</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3.1</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3.4</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">17.0</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">0</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">0</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3</span></p></td><td class=\"cl-df926632\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">2</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">18.1</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">6</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">225</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">105</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">2.8</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3.5</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">20.2</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">1</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">0</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">3</span></p></td><td class=\"cl-df92663c\"><p class=\"cl-df922488\"><span class=\"cl-df92142a\">1</span></p></td></tr></tbody></table></div>\n```\n\n"
#> attr(,"class")
#> [1] "knit_asis"
#> attr(,"knit_cacheable")
#> [1] NA