flextable R Package

repository·main·Indexed 20 days ago

https://github.com/davidgohel/flextable

An R package for creating highly customizable, publication-quality tables. It provides a framework to format data frames using a set of verbs and export them to Word, HTML, PDF, PowerPoint, RTF, and image formats (png, svg). Key features include cell highlighting, background coloring, multi-line headers, and the ability to convert various R objects into tables via as_flextable().

Tokens
795
Snippets
4
Records
5
Agent score
21%

What's inside flextable

  1. What is a flextable and how does it work?

    main

    A flextable object is a data.frame representation that provides an API for controlling formatting properties and layout. It is designed for creating tables for reporting and publications.

    Key features include:

    • Multi-line headers: Support for complex header structures.
    • Cell customization: Ability to merge cells or include images within cells.
    • Versatile output: Tables can be embedded in HTML, PDF, Word, PowerPoint, RTF, or exported as images (png, svg). They can also be used in patchwork layouts with ggplot2 plots.
  2. Create and export a flextable

    main

    The core workflow involves creating a flextable object from a data.frame (or using as_flextable() to convert other objects), applying formatting verbs, and then exporting to a desired format. You can export to Word (.docx) using save_as_docx().

    library(flextable)
    
    flextable(mtcars) |> 
      theme_vanilla() |> 
      save_as_docx(path = "mytable.docx")
  3. Format flextable cells and rows

    main

    Formatting is applied using a set of verbs that can be layered on a flextable object. Key capabilities include:

    • Highlighting: Use highlight() to apply colors to specific cells based on conditions.
    • Background colors: Use bg() to set background colors for specific columns (j).
    • Footers: Use add_footer_lines() to add text to the bottom of the table.
    • General formatting: Control text, paragraphs, cells, borders, and displayed values.
    flextable(head(mtcars)) |> 
      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")
  4. Install the flextable R package

    main

    You can install the stable version of flextable from CRAN using install.packages(). If you need the development version, you can install it from GitHub using devtools::install_github().

    # Install from CRAN
    install.packages("flextable")
    
    # Install development version from GitHub
    devtools::install_github("davidgohel/flextable")
  5. Convert objects to flextable using as_flextable()

    main

    The as_flextable() function allows you to convert various R objects (such as those returned by table() or statistical models) into a flextable object. This is useful for creating publication-quality tables from summary statistics or model outputs.

    # Example using a hypothetical summarizor output
    ggplot2::diamonds[, c("cut", "carat", "price", "clarity", "table")] |> 
      summarizor(by = c("cut")) |> 
      as_flextable(spread_first_col = TRUE)