Skip to content

Instantly share code, notes, and snippets.

@yjunechoe
Last active July 28, 2023 16:50
Show Gist options
  • Save yjunechoe/a95c9d6bb60080d574cfbd74177d413b to your computer and use it in GitHub Desktop.
Save yjunechoe/a95c9d6bb60080d574cfbd74177d413b to your computer and use it in GitHub Desktop.
Make {camcorder} work for {gt}
# gt-camcorder prototype using examples from {gt} vignette
# -- https://gt.rstudio.com/articles/creating-summary-lines.html
library(devtools)
dev_mode(on = TRUE)
# Install my fork in dev-mode with gt camcorder draft
remotes::install_github("yjunechoe/camcorder@gt-support")
library(camcorder)
library(gt)
library(dplyr)
# Record setup ----
gg_record(
dir = file.path(tempdir(), "recording100"),
device = "png",
width = 4,
height = 6,
units = "in",
scale = 2
)
# A whole buncha tables ----
# Ignore the "screenshot completed" messages - it's a known issue
# -- https://github.com/rstudio/webshot2/issues/24
exibble |> gt()
exibble_a <-
exibble |>
select(-c(fctr, date, time, datetime)) |>
gt(rowname_col = "row", groupname_col = "group") |>
sub_missing()
exibble_a
exibble_b <-
exibble_a |>
summary_rows(
groups = everything(),
columns = num,
fns = list(
average = "mean",
total = "sum",
SD = "sd"
)
)
exibble_b
exibble_c <-
exibble_a |>
summary_rows(
groups = everything(),
columns = num,
fns = list(
avg = ~ mean(., na.rm = TRUE),
total = ~ sum(., na.rm = TRUE),
s.d. = ~ sd(., na.rm = TRUE)
)
)
exibble_c
fns_labels <-
list(
avg = ~mean(., na.rm = TRUE),
total = ~sum(., na.rm = TRUE),
s.d. = ~sd(., na.rm = TRUE)
)
exibble_d <-
exibble_a |>
fmt_scientific(
columns = num,
decimals = 3
) |>
summary_rows(
groups = everything(),
columns = num,
fns = fns_labels,
fmt = list(~ fmt_scientific(., decimals = 3))
)
exibble_d
exibble_e <-
exibble_a |>
fmt_scientific(
columns = num,
decimals = 3
) |>
fmt_currency(
columns = currency,
currency = "EUR"
) |>
summary_rows(
groups = everything(),
columns = num,
fns = fns_labels,
fmt = list(~ fmt_scientific(., decimals = 3))
) |>
summary_rows(
groups = "grp_a",
columns = currency,
fns = c(
fns_labels,
min = ~ min(.),
max = ~ max(.)
),
fmt = list(~ fmt_currency(., currency = "EUR"))
)
exibble_e
formats <-
list(
decimals = 3,
locale = "fr_BE",
currency = "EUR"
)
exibble_f <-
exibble_a |>
fmt_scientific(
columns = num,
decimals = formats$decimals,
locale = formats$locale
) |>
fmt_currency(
columns = currency,
currency = formats$currency,
locale = formats$locale
) |>
summary_rows(
groups = everything(),
columns = num,
fns = fns_labels,
fmt = list(~ fmt_scientific(.,
decimals = formats$decimals,
locale = formats$locale
))
) |>
summary_rows(
groups = "grp_a",
columns = currency,
fns = c(
fns_labels,
min = ~min(.),
max = ~max(.)
),
fmt = list(~ fmt_currency(.,
currency = formats$currency,
locale = formats$locale
))
)
exibble_f
exibble_g <-
exibble |>
select(num, char, currency) |>
gt() |>
grand_summary_rows(
columns = c(num, currency),
fns = fns_labels
)
exibble_g
exibble_h <-
exibble_f |>
grand_summary_rows(
columns = num,
fns = fns_labels,
fmt = list(~ fmt_number(.,
suffixing = TRUE,
locale = formats$locale
))
) |>
grand_summary_rows(
columns = currency,
fns = fns_labels,
fmt = list(~ fmt_currency(.,
suffixing = TRUE,
locale = formats$locale
))
)
exibble_h
exibble_i <-
exibble_h |>
tab_options(
summary_row.background.color = "lightblue",
grand_summary_row.background.color = "lightgreen"
) |>
tab_footnote(
footnote = md("Mean of all *num* values."),
locations = cells_grand_summary(
columns = "num", rows = "avg"
)
) |>
tab_footnote(
footnote = md("Highest `currency` value in **grp_a**"),
locations = cells_summary(
groups = "grp_a",
columns = "currency",
rows = "max"
)
)
exibble_i
## Save gif ----
gg_playback(
name = file.path(tempdir(), "recording100", "vignette_gif.gif"),
first_image_duration = 3,
last_image_duration = 3,
frame_duration = .5,
image_resize = 1800
)
dev_mode(on = FALSE)
@yjunechoe
Copy link
Author

vignette_gif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment