Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sharlagelfand/165825299f9a28c768c4cc4b55471919 to your computer and use it in GitHub Desktop.
Save sharlagelfand/165825299f9a28c768c4cc4b55471919 to your computer and use it in GitHub Desktop.
{gt} x #TwoFunctionsMostDays wrapup
library(gt)
library(tibble)
acnh_bugs_n <- tribble(
~name, ~price, ~location, ~time, ~months, ~image,
"Yellow butterfly", 160, "Flying", "4 AM - 7 PM", "Mar - Jun, Sep - Oct", "https://vignette.wikia.nocookie.net/animalcrossing/images/f/fa/NH-Icon-yellowbutterfly.png",
"Peacock butterfly", 2500, "Flying by Hybrid Flowers", "4 AM - 7 PM", "Mar - June", "https://vignette.wikia.nocookie.net/animalcrossing/images/8/8f/NH-Icon-peacockbutterfly.png",
"Atlas moth", 3000, "On Trees", "7 PM - 4 AM", "Apr - Sep", "https://vignette.wikia.nocookie.net/animalcrossing/images/6/6a/NH-Icon-atlasmoth.png",
"Centipede", 300, "Hitting Rocks", "4 PM - 11 PM", "Sep - June", "https://vignette.wikia.nocookie.net/animalcrossing/images/3/30/NH-Icon-centipede.png",
"Snail", 250, "On Rocks and Bushes (Rain) ", "All Day", "Jan - Dec", "https://vignette.wikia.nocookie.net/animalcrossing/images/b/b1/NH-Icon-snail.png",
"Mole cricket", 500, "Underground", "All Day", "Nov - May", "https://vignette.wikia.nocookie.net/animalcrossing/images/0/00/NH-Icon-molecricket.png"
)
gt(acnh_bugs_n) %>%
# Use number formatting with comma separator for thousands and no decimals
fmt_number(columns = vars(price), decimals = 0) %>%
# Convert URL in image variable to an actual image
text_transform(
locations = cells_body(
columns = vars(image)
),
fn = function(x) {
web_image(x, height = 50)
}
) %>%
# And move that column to the front!
cols_move_to_start(columns = vars(image)) %>%
# Capitalize columns without changing underlying data
tab_options(column_labels.text_transform = "capitalize") %>%
# Add a heading spanner above variables time and months
tab_spanner(
label = "Availability",
columns = vars(time, months)
) %>%
# Bold column labels and spanners
tab_style(
style = cell_text(weight = "bold"),
locations = list(
cells_column_labels(columns = everything()),
cells_column_spanners("Availability")
)
) %>%
# Add a footnote on the Price column label and change the footnote marker
tab_footnote(
footnote = "Price is in Bells",
locations = cells_column_labels(columns = vars(price))
) %>%
opt_footnote_marks(marks = "standard") %>%
# Add a source note, using markdown!
tab_source_note(md("Source: Daniel Chen's [{animalcrossing} package](https://github.com/chendaniely/animalcrossing)")) %>%
# Add a heading and subtitle
tab_header(
title = "Animal Crossing New Horizons Critters",
subtitle = "(Availability based on Northern Hemisphere)"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment