Created
September 13, 2024 23:16
-
-
Save JoFrhwld/f336b2632e11585f1ff11e69096a315a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(gt) | |
tibble( | |
value = 1:6, | |
name = c( | |
"one", "two", "three", | |
"four", "five", "six" | |
), | |
die = str_glue( | |
"dice-{name}" | |
) | |
) -> | |
dice_values | |
expand_grid( | |
die1_v = 1:6, | |
die2_v = 1:6 | |
) |> | |
mutate( | |
total = die1_v + die2_v | |
) |> | |
left_join( | |
dice_values, | |
by = join_by(die1_v == value) | |
) |> | |
left_join( | |
dice_values, | |
by = join_by(die2_v == value) | |
) |> | |
select( | |
-starts_with("name"), | |
-ends_with("_v") | |
) |> | |
pivot_longer( | |
die.x:die.y | |
) |> | |
mutate( | |
.by = c(total, name), | |
combo = row_number(), | |
n = n() | |
) |> | |
unite( | |
c(name, combo), | |
col = "dice_combo", | |
sep = "_" | |
) |> | |
pivot_wider( | |
names_from = dice_combo, | |
values_from = value | |
) |> | |
relocate( | |
n, | |
.after = last_col() | |
) |> | |
mutate( | |
probability = n/sum(n) | |
) |> | |
gt() |> | |
fmt_icon( | |
contains(".x_"), | |
fill_color = "#4477AA" | |
) |> | |
fmt_icon( | |
contains(".y_"), | |
fill_color = "#cc6677" | |
) |> | |
sub_missing( | |
missing_text = "" | |
) |> | |
cols_merge( | |
ends_with("_1") | |
) |> | |
cols_merge( | |
ends_with("_2") | |
) |> | |
cols_merge( | |
ends_with("_3"), | |
) |> | |
cols_merge( | |
ends_with("_4"), | |
) |> | |
cols_merge( | |
ends_with("_5"), | |
) |> | |
cols_merge( | |
ends_with("_6"), | |
) |> | |
cols_label( | |
starts_with("die") ~ "" | |
) |> | |
grand_summary_rows( | |
columns = c(n, probability), | |
fns = list( | |
total ~ sum(.) | |
), | |
missing_text = "" | |
) |> | |
fmt_number( | |
columns = probability, | |
decimals = 2 | |
) |> | |
opt_table_font( | |
font = c( | |
google_font(name = "Public Sans"), | |
default_fonts() | |
) | |
) |> | |
tab_style( | |
style = " | |
background-color: var(--bs-body-bg); | |
color: var(--bs-body-color) | |
", | |
locations = list( | |
cells_column_labels(), | |
cells_column_spanners(), | |
cells_row_groups(), | |
cells_body(), | |
cells_grand_summary(), | |
cells_stub_grand_summary(), | |
cells_stub(), | |
cells_stubhead() | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment