kableExtra R Package Documentation

repository·master·Indexed 20 days ago

https://github.com/haozhu233/kableextra

An R package that extends knitr::kable() to create highly customized tables for HTML, LaTeX, PDF, and EPUB outputs. It provides a pipe-friendly syntax and unified functions like kbl() and kable_styling() to apply styling and structural elements across different formats. The documentation includes guides for configuring bookdown projects, enabling Bootstrap tables in Gitbook and Epub, and managing cross-format table generation.

Tokens
2.1K
Snippets
8
Records
9
Agent score
73%

What's inside kableExtra

  1. How kableExtra works with kable()

    master

    kableExtra is not a standalone table generator. Instead, it is an extension package designed to add features to the output of knitr::kable() (or the kbl() alternative) using the R pipe operator (%>%).

    Key concepts:

    • Pipable syntax: You chain kableExtra functions onto a table object to layer styling and structural elements.
    • Unified functions: Most functions are format-agnostic. They detect whether you are rendering to HTML or LaTeX (based on the format specified in kable() or the global knitr.table.format option) and apply the appropriate styling automatically.
    • kbl() function: A recommended alternative to kable() that provides better documentation and improved format detection.
    library(kableExtra)
    dt <- mtcars[1:5, 1:4]
    
    # Example of the pipable pattern
    kbl(dt) %>%
      kable_styling(bootstrap_options = "striped") %>%
      footnote(c("table footnote"))
  2. Prepare tables for multiple formats (HTML, LaTeX, EPUB)

    master

    To ensure tables look good across different output formats, you must combine universal kable and kableExtra API calls with format-specific settings.

    Key considerations:

    • Universal API: Most styling functions work similarly for both HTML and LaTeX.
    • Format-specific settings:
      • Use booktabs = T and longtable settings within kable() for LaTeX.
      • Use bootstrap_options (for HTML) and latex_options (for LaTeX) within kable_styling().
    • EPUB Support: If you need to output tables in .epub, ensure you are using the development version or version 1.0 on CRAN, as older versions (below 0.9.0) may not support it.
    library(kableExtra)
    library(dplyr)
    
    # Example of a cross-format compatible table
    options(kableExtra.html.bsTable = T)
    iris[1:10, ] %>%
      mutate_if(is.numeric, function(x) {
        cell_spec(x, bold = T, 
                  color = spec_color(x, end = 0.9), 
                  font_size = spec_font_size(x))
      }) %>%
      mutate(Species = cell_spec(
        Species, color = "white", bold = T,
        background = spec_color(1:10, end = 0.9, 
                                option = "A", direction = -1)
      )) %>%
      kable(escape = F, align = "c", booktabs = T) %>%
      kable_styling(c("striped", "condensed"), 
                    latex_options = "striped", 
                    full_width = F)
  3. Use Bootstrap Tables in Gitbook

    master

    When using bookdown to create Gitbooks, rmarkdown does not load bootstrap by default. To enable Bootstrap styling for your kableExtra tables, you must set the kableExtra.html.bsTable option to TRUE.

    Important: Gitbook's default table CSS can conflict with Bootstrap (e.g., preventing the hover effect from working). To resolve this, ensure you are using bookdown version 0.7.21 or later and disable the default table CSS in your _output.yml file.

    Steps to enable Bootstrap in Gitbook:

    1. Set the kableExtra option in your R script.
    2. Disable table_css in your _output.yml configuration.
    library(kableExtra)
    options(kableExtra.html.bsTable = T)
    
    mtcars[1:5, 1:5] %>%
      kable(booktabs = T) %>%
      kable_styling(
        bootstrap_options = c("striped","hover", "bordered", "condensed"),
        latex_options = c("striped")
      ) %>%
      column_spec(1, color = "red") %>%
      add_header_above(c(" ", "Group A" = 2, "Group B" = 3))

    Configure _output.yml for Gitbook

    bookdown::gitbook:
      table_css: false
  4. Install kableExtra

    master

    You can install the stable version of kableExtra from CRAN or the development version from GitHub using devtools.

    # Stable version
    install.packages("kableExtra")
    
    # Dev version
    devtools::install_github("haozhu233/kableExtra")
  5. Use Bootstrap Tables in Epub

    master

    Currently, you cannot load additional CSS through HTML dependencies in Epub output. To use Bootstrap-styled tables in an Epub book, you must manually include a stylesheet.

    Steps to enable Bootstrap in Epub:

    1. Download the bootstrapTable.min.css file.
    2. Save it as a local CSS file (e.g., style.css).
    3. Reference this file in your _output.yml under the bookdown::epub_book configuration.
    bookdown::epub_book: 
      stylesheet: style.css
  6. Use kableExtra in Bookdown projects

    master
    When working with bookdown projects that require multiple output formats (such as HTML, PDF, and EPUB), kableExtra provides unified functions that allow you to use the same piece of code to render tables across different formats. This addresses common issues where code might work for HTML but fail for PDF, or where specific styling (like bootstrap style tables) is needed in formats like gitbook.
  7. Configure Bookdown for cross-format tables

    master

    When using kableExtra in a multi-format bookdown project (e.g., generating both HTML and PDF), you must use the "M-K" approach. This is achieved by setting new_session: true in your _bookdown.yml file.

    Setting new_session: true forces R to use a new session for every chapter, ensuring that tables are generated correctly for each specific format. Without this, the global environment might be shared across formats, causing styling issues.

    Note: The "M-K" approach is slower than the "K-M" approach, and packages/data are not shared across chapters.

    book_filename: "bookdown_example"
    delete_merged_file: true
    new_session: true
    language:
      ui:
        chapter_name: "Chapter "
  8. Generate a LaTeX table

    master

    To create a styled LaTeX table, use kbl() with booktabs = T and pipe the result into kable_styling() using the latex_options argument.

    library(kableExtra)
    dt <- mtcars[1:5, 1:4]
    
    kbl(dt, booktabs = T, caption = "Demo Table") %>%
      kable_styling(latex_options = c("striped", "hold_position"),
                    full_width = F) %>%
      add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
      footnote(c("table footnote"))
  9. Generate an HTML table

    master

    To create a styled HTML table, use kbl() (or kable()) and pipe the result into kable_styling() with bootstrap_options. You can also add headers and footnotes.

    library(kableExtra)
    dt <- mtcars[1:5, 1:4]
    
    kbl(dt, caption = "Demo Table") %>%
      kable_styling(bootstrap_options = "striped",
                    full_width = F) %>%
      add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
      footnote(c("table footnote"))