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-df62ae3c{}.cl-df561d3e{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-df561d52{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-df5b5a1a{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-df5b7c5c{width:0.911in;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-df5b7c66{width:0.911in;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;}.cl-df5b7c70{width:0.911in;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-df5b7c7a{width:0.911in;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;}</style><table class='cl-df62ae3c'><thead><tr style=\"overflow-wrap:break-word;\"><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">mpg</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">cyl</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">disp</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">hp</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">drat</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">wt</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">qsec</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">vs</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">am</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">gear</span></p></th><th class=\"cl-df5b7c5c\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">carb</span></p></th></tr><tr style=\"overflow-wrap:break-word;\"><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th><th class=\"cl-df5b7c66\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d52\">numeric</span></p></th></tr></thead><tbody><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">21.0</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">6</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">160</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">110</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3.9</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">2.6</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">16.5</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">0</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">1</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">4</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">4</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">21.0</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">6</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">160</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">110</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3.9</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">2.9</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">17.0</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">0</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">1</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">4</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">4</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">22.8</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">4</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">108</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">93</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3.9</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">2.3</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">18.6</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">1</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">1</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">4</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">1</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">21.4</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">6</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">258</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">110</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3.1</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3.2</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">19.4</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">1</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">0</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">1</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">18.7</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">8</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">360</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">175</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3.1</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3.4</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">17.0</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">0</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">0</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3</span></p></td><td class=\"cl-df5b7c70\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">2</span></p></td></tr><tr style=\"overflow-wrap:break-word;\"><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">18.1</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">6</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">225</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">105</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">2.8</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3.5</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">20.2</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">1</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">0</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">3</span></p></td><td class=\"cl-df5b7c7a\"><p class=\"cl-df5b5a1a\"><span class=\"cl-df561d3e\">1</span></p></td></tr></tbody></table></div>\n```\n\n\n\n"
#> attr(,"class")
#> [1] "knit_asis"
#> attr(,"knit_cacheable")
#> [1] NA