### Install gt Development Version from GitHub
Source: https://github.com/rstudio/gt/blob/master/README.md
Install the latest development version of the gt package directly from its GitHub repository. This is useful for accessing the newest features or bug fixes.
```r
devtools::install_github("rstudio/gt")
```
--------------------------------
### HTML Table Output with Summary Rows
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/summary_rows.md
Example of an HTML table generated by the gt package, showcasing summary rows and grouped data.
```html
open
high
low
close
W02
2015-01-05
2054.44
2054.44
2017.34
2020.58
2015-01-06
2022.15
2030.25
1992.44
2002.61
2015-01-07
2005.55
2029.61
2005.55
2025.90
2015-01-08
2030.61
2064.08
```
--------------------------------
### Install gt from CRAN
Source: https://github.com/rstudio/gt/blob/master/README.md
Use this command to install the stable version of the gt package from CRAN.
```r
install.packages("gt")
```
--------------------------------
### Create a gt Table from Data
Source: https://github.com/rstudio/gt/blob/master/README.md
This example demonstrates the basic usage of the gt package to create a table from a dataset. Ensure the 'gt' library is loaded and data is prepared.
```r
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
```
--------------------------------
### HTML Table Rendering with fmt_auto()
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/fmt_auto.md
This example shows the HTML output generated by `render_as_html()` when `fmt_auto()` has been applied. It highlights the automatic formatting of different numeric types.
```R
cat(render_as_html(gt_tbl))
```
```html
integer
amt.usd
sold_EUR
real
real_small
100
$23.23
€3,465.20
23.45
23.45
8,000
$632.32
€8,743.30
0.23
352.4
4 × 106
$236.34
€367.02
1.000 × 10−6
0.013
3 × 108
$12.00
€23.74
2.332 × 106
263.8
```
--------------------------------
### Using summary_rows() without row groups (deprecated)
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/summary_rows.md
This example demonstrates the deprecated usage of `gt::summary_rows()` when no row groups are defined. It will now raise an error, guiding users to `grand_summary_rows()` or to add row groups.
```r
gt::summary_rows(gt::gt(dplyr::select(mtcars_short, gear), rownames_to_stub = TRUE),
fns = "sum")
```
--------------------------------
### Render Function Notation with LaTeX
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/fmt_markdown.md
This example demonstrates rendering a function notation, f(ct), using LaTeX within an HTML context. It includes the KaTeX CSS link and the MathML/HTML representation of the function.
```html
f(ct)
```
--------------------------------
### Apply a Predefined Style Theme with opt_stylize()
Source: https://context7.com/rstudio/gt/llms.txt
Quickly applies one of six preset visual styles combined with six color variations. Covers background colors, border styles, striping, and stub shading.
```r
exibble |>
gt(rowname_col = "row", groupname_col = "group") |>
summary_rows(
groups = "grp_a",
columns = c(num, currency),
fns = c("min", "max")
) |>
opt_stylize(
style = 6,
color = "cyan",
add_row_striping = TRUE
)
```
--------------------------------
### Set Key and Subset Data with data.table
Source: https://github.com/rstudio/gt/blob/master/tests/performance-monitoring/dplyr-known-good.md
This example demonstrates how to set a key on a data.table for efficient subsetting. The `setkey` function prepares the data for fast lookups, and the subsequent subsetting operation `MT[.(6, 4)]` is equivalent to a more verbose `MT[cyl == 6 & gear == 4]` but significantly faster for large datasets.
```r
setkey(MT, cyl, gear)
MT[.(6, 4)] # Equivalent to MT[cyl == 6 & gear == 4]
```
--------------------------------
### Example Word OOXML Output for gt Table
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/as_word.md
This is an example of the Word OOXML output generated for a gt table. It includes elements for table captions, titles, and the table content itself, with specific formatting applied to each part.
```XML
Table SEQ Table \* ARABIC 1: TABLETITLEtablesubtitlenum
```
--------------------------------
### Example Word OOXML Table Structure
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/as_word.md
This is an example of the Word OOXML structure generated for a table. It includes definitions for table properties, rows, cells, and paragraphs within cells, specifying formatting like borders, spacing, fonts, and text content.
```XML
row_1grp_a
```
--------------------------------
### Basic Decimal Alignment Example
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/cols_align_decimal.md
Shows a table with a numeric column aligned to the right, demonstrating basic decimal alignment. Includes currency symbols, parentheses for negative values, and NA values.
```html
$9,023
D
($284)
E
NA
F
$0
G
($123)
H
NA
I
$41
```
--------------------------------
### Render Fraction Formula in HTML
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/fmt_markdown.md
This snippet demonstrates rendering a fraction and a function in HTML using KaTeX. It includes a link to the KaTeX CSS for proper styling, suitable for displaying mathematical formulas in web pages.
```html
t1f(t)
```
--------------------------------
### Filter data.frame rows
Source: https://github.com/rstudio/gt/blob/master/tests/performance-monitoring/dplyr-known-good.md
This example demonstrates filtering rows from a data.frame based on multiple conditions using the `filter` function.
```r
mtcars |> filter(cyl >= 6 & disp < 180)
```
--------------------------------
### str_substitute Recycling Errors
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/utils.md
Demonstrates errors that occur when recycling arguments in str_substitute. Ensure input vectors have compatible lengths for 'start' and 'end' arguments.
```R
str_substitute(c("223", "223", "224"), c(1, 2), 2)
```
```R
str_substitute(c("223", "223", "224"), c(1), c(2, 3))
```
```R
str_substitute(c("223", "223", "224", "225"), c(1, 2, 3, 4), c(2, 3))
```
--------------------------------
### Create Higher-Order Spanners with tab_spanner_delim()
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/tab_spanner_delim.md
This example shows how to use tab_spanner_delim() to create multi-level column spanners. The delimited strings define the hierarchy and labels for the spanners.
```r
gt_tbl %>%
tab_spanner_delim(delim = ".")
```
--------------------------------
### Render LaTeX Formula in Markdown
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/fmt_markdown.md
Demonstrates rendering a LaTeX formula for a fraction within a Markdown context. Ensure KaTeX CSS is linked for proper display.
```html
(s2+a2)22as
```
--------------------------------
### Resolve rows by name in data_color()
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/data_color.md
Shows how data_color() resolves rows by name, similar to fmt_number(). This example demonstrates an error when the specified row name does not exist.
```R
fmt_number(gt_simple, rows = "Datsun 710", decimals = 6)
```
```R
data_color(gt_simple, rows = "Datsun 710")
```
--------------------------------
### Render LaTeX Formulas in HTML
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/fmt_markdown.md
Demonstrates how to render LaTeX formulas within HTML output using the gt package. Ensure the KaTeX library is linked for proper rendering.
```html
sin(at)+atcos(at)
```
--------------------------------
### Render Trigonometric Function in Markdown
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/fmt_markdown.md
Shows how to render a trigonometric function with arguments in Markdown using KaTeX. Includes linking the KaTeX CSS for rendering.
```html
tcos(at)
```
--------------------------------
### Retrieve Ordering Keys from Data.table in R
Source: https://github.com/rstudio/gt/blob/master/tests/performance-monitoring/dplyr-known-good.md
Use `key()` to get the names of the columns by which the data.table is currently ordered. This helps in understanding the current data structure.
```r
key(MT)
```
--------------------------------
### Render HTML Table with Rows
Source: https://github.com/rstudio/gt/blob/master/tests/testthat/_snaps/rows_add.md
This snippet demonstrates how to render a gt table as HTML. It includes examples of column headings and row data, including grouped rows.
```html
num
char
fctr
date
time
datetime
currency
grp_a
row_1
1.111e-01
apricot
one
2015-01-15
13:35
2018-01-01 02:22
49.950
row_2
2.222e+00
banana
two
2015-02-15
14:40
2018-02-02 14:33
17.950
row_3
3.333e+01
coconut
three
2015-03-15
15:45
2018-03-03 03:44
1.390
row_4
4.444e+02
durian
four
2015-04-15
16:50
2018-04-04 15:55
65100.000
grp_b
row_5
5.550e+03
NA
five
2015-05-15
17:55
2018-05-05 04:00
1325.810
```
--------------------------------
### Format date values with fmt_date()
Source: https://context7.com/rstudio/gt/llms.txt
Use fmt_date to format Date, POSIXt, or character date values. Supports 41 built-in date styles. Use info_date_style() to see all available styles. This example also demonstrates formatting a currency column.
```r
sp500 |
filter(date >= "2010-06-07", date <= "2010-06-14") |
select(date, close) |
gt() |
fmt_date(
columns = date,
date_style = "wd_m_day_year" # e.g. "Monday, June 7, 2010"
) |
fmt_currency(columns = close)
```