Formatting Dataframes with Flextable: Enhance Table Presentation
Learn how to use Flextable, a versatile package for formatting dataframes into professional tables for reports and presentations. Discover the features such as altering headers, alignment, changing cell sizes, text formatting, table styling, and supported output formats like RMarkdown, Shiny, and more. Follow step-by-step instructions to customize tables and improve visual appeal with font styles, colors, and conditional formatting."
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Flextable Neil Bray
Who Author is David Gohel, works for data company called ArData First version created 28/03/2017 Updated 15 times Current version: 0.5.6 (released 12/11/2019) Available from CRAN, development version from GitHub
What The package allows the formatting of dataframes so they are suitable to be used as tables in reports/presentations. Layout (alter header rows, alignment, change cell height and width, and merge cells) Text formatting (font type, bold, italic, size, color) Table Formatting (background colour, borders, conditional formatting) Supported output: RMarkdown (HTML, Word, PDF and PowerPoint), Shiny, image file, R graphic, viewed directly in Word/PowerPoint
How library(flextable); data <- iris[c(1:3, 51:53, 101:103),] ft <- flextable(data, col_keys = c("Species", "Sepal.Length", "Petal.Length")) Changed header names Merged cells Aligned right species col Change column widths Changed font size Changed font type Emboldened header Changed font colour Shaded header light blue Conditional formatting Added border line
How - layout #change header names ft <- set_header_labels(ft, Sepal.Length = "Sepal Length") ft <- set_header_labels(ft, Petal.Length = "Petal Length") #merge adjacent cells with same name in 1stcolumn ft <- merge_v(ft, j = ~Species) ft <- fix_border_issues(ft) #align ft <- align(ft, j=1, align = "right", part = "body") ft <- align(ft, j=2:3, align="center",part="header") #change column widths and row heights ft <-width(ft, j = 2:3, width=0.7) ft <- height(ft,height=0.6,part="header")
How text formatting #change font size ft <- fontsize(ft, size=14, part="header") #change font ft <- font(ft, fontname="Calibri", part = "all") #make header bold ft <- bold(ft, part="header") #change font colour ft <- color(ft, color="blue",part="body")
How table formatting #specify background colour ft <- bg(ft, bg="white",part="body") #make header colour light blue ft <- bg(ft, bg="#add8e6",part="header") #make cells containing a value of >6 orange (sepal length column) ft <-bg(ft, i = ~ Sepal.Length >6,j= ~ Sepal.Length,bg = "orange") #add grey line to separate the species greyline <- officer::fp_border(color="dark grey", width=2) ft <-hline(ft, i = c(3,6), border = greyline, part = "body")
How Changed header names Merged cells Aligned right species col Change column widths Changed font size Changed font type Emboldened header Changed font colour Shaded header light blue Conditional formatting Added border line
How Flextables can be used in: RMarkdown documents (Word, PowerPoint, HTML, PDF) Shiny (use uiOutput for UI definition, renderUI in the server function) Saved as an image file: save_as_image(ft, path = "ft.png") Rendered as an R graphic: plot(ft) Viewed directly in Word/PowerPoint: print(ft, preview = "docx")
And now... Practice by choosing a table and trying to replicate it using Flextable! Useful resources: https://davidgohel.github.io/flextable/ https://cran.r-project.org/web/packages/flextable/index.html https://github.com/davidgohel/flextable