gt R Package Documentation

repository·master·Indexed 24 days ago

https://github.com/rstudio/gt

An R package for creating highly customizable, professional-looking tables from data frames or tibbles. It utilizes a cohesive set of table parts—including headers, stubs, bodies, and footers—to build complex tables. Supports rendering to HTML, LaTeX, and RTF formats.

Tokens
577
Snippets
2
Records
4
Agent score
30%

What's inside gt

  1. How gt tables are constructed and rendered

    master

    The gt workflow follows a consistent pattern:

    1. Table Data: Start with a tibble or data.frame.
    2. Composition: Use gt() to create the table object, then compose it using a cohesive set of table parts such as the table header, stub, column labels, spanner column labels, table body, and table footer.
    3. Rendering: Render the table by printing it to the console, including it in an R Markdown document, or exporting it to a file using gtsave().

    Supported output formats include HTML, LaTeX, and RTF.

  2. Install the gt package

    master

    You can install the stable version of gt from CRAN or the development version from GitHub.

    From CRAN:

    install.packages("gt")

    From GitHub (development version):

    devtools::install_github("rstudio/gt")
    install.packages("gt")
  3. Create a gt table with formatting

    master

    This example demonstrates the standard workflow: filtering data, initializing a gt table, adding a header, and applying specific formatting to columns (currency, dates, and numbers).

    library(gt)
    
    # Define the start and end dates for the data range
    start_date <- "2010-06-07"
    end_date <- "2010-06-14"
    
    # Create a gt table based on preprocessed
    # `sp500` table data
    sp500 |>
      dplyr::filter(date >= start_date & date <= end_date) |>
      dplyr::select(-adj_close) |>
      gt() |>
      tab_header(
        title = "S&P 500",
        subtitle = glue::glue("{start_date} to {end_date}")
      ) |>
      fmt_currency() |>
      fmt_date(columns = date, date_style = "wd_m_day_year") |>
      fmt_number(columns = volume, suffixing = TRUE)