|
| 1 | +--- |
| 2 | +title: "knitr dark mode - gt" |
| 3 | +brand: |
| 4 | + light: united-brand.yml |
| 5 | + dark: slate-brand.yml |
| 6 | +execute: |
| 7 | + echo: false |
| 8 | + warning: false |
| 9 | +--- |
| 10 | + |
| 11 | +```{r} |
| 12 | +#| echo: false |
| 13 | +#| warning: false |
| 14 | +library(gt) |
| 15 | +library(quarto) |
| 16 | +
|
| 17 | +united_theme <- theme_brand_gt('united-brand.yml') |
| 18 | +slate_theme <- theme_brand_gt('slate-brand.yml') |
| 19 | +``` |
| 20 | + |
| 21 | +```{r} |
| 22 | +#| renderings: [light, dark] |
| 23 | +library(tidyverse) |
| 24 | +penguins <- palmerpenguins::penguins |> filter(!is.na(sex)) |
| 25 | +penguin_counts <- penguins |> |
| 26 | + mutate(year = as.character(year)) |> |
| 27 | + group_by(species, island, sex, year) |> |
| 28 | + summarise(n = n(), .groups = 'drop') |
| 29 | +penguin_counts_wider <- penguin_counts |> |
| 30 | + pivot_wider( |
| 31 | + names_from = c(species, sex), |
| 32 | + values_from = n |
| 33 | + ) |> |
| 34 | + # Make missing numbers (NAs) into zero |
| 35 | + mutate(across(.cols = -(1:2), .fns = ~replace_na(., replace = 0))) |> |
| 36 | + arrange(island, year) |
| 37 | +
|
| 38 | +actual_colnames <- colnames(penguin_counts_wider) |
| 39 | +desired_colnames <- actual_colnames |> |
| 40 | + str_remove('(Adelie|Gentoo|Chinstrap)_') |> |
| 41 | + str_to_title() |
| 42 | +names(desired_colnames) <- actual_colnames |
| 43 | +
|
| 44 | +
|
| 45 | +penguins_table <- penguin_counts_wider |> |
| 46 | + mutate(across(.cols = -(1:2), ~if_else(. == 0, NA_integer_, .))) |> |
| 47 | + mutate( |
| 48 | + island = as.character(island), |
| 49 | + year = as.numeric(year), |
| 50 | + island = paste0('Island: ', island) |
| 51 | + ) |> |
| 52 | + gt(groupname_col = 'island', rowname_col = 'year') |> |
| 53 | + cols_label(.list = desired_colnames) |> |
| 54 | + tab_spanner( |
| 55 | + label = md('**Adelie**'), |
| 56 | + columns = 3:4 |
| 57 | + ) |> |
| 58 | + tab_spanner( |
| 59 | + label = md('**Chinstrap**'), |
| 60 | + columns = c('Chinstrap_female', 'Chinstrap_male') |
| 61 | + ) |> |
| 62 | + tab_spanner( |
| 63 | + label = md('**Gentoo**'), |
| 64 | + columns = contains('Gentoo') |
| 65 | + ) |> |
| 66 | + tab_header( |
| 67 | + title = 'Penguins in the Palmer Archipelago', |
| 68 | + subtitle = 'Data is courtesy of the {palmerpenguins} R package' |
| 69 | + ) |> |
| 70 | + sub_missing(missing_text = '-') |> |
| 71 | + summary_rows( |
| 72 | + groups = TRUE, |
| 73 | + fns = list( |
| 74 | + 'Maximum' = ~max(.), |
| 75 | + 'Total' = ~sum(.) |
| 76 | + ), |
| 77 | + formatter = fmt_number, |
| 78 | + decimals = 0, |
| 79 | + missing_text = '-' |
| 80 | + ) |> |
| 81 | + tab_options( |
| 82 | + data_row.padding = px(2), |
| 83 | + summary_row.padding = px(3), # A bit more padding for summaries |
| 84 | + row_group.padding = px(4) # And even more for our groups |
| 85 | + ) |> |
| 86 | + opt_stylize(style = 6, color = 'gray') |
| 87 | +
|
| 88 | +penguins_table |> united_theme() |
| 89 | +
|
| 90 | +penguins_table |> slate_theme() |
| 91 | +``` |
| 92 | + |
| 93 | +Here's a [link](https://example.com). |
| 94 | + |
| 95 | +{{< lipsum 3 >}} |
0 commit comments