Skip to content

Instantly share code, notes, and snippets.

View rich-iannone's full-sized avatar

Richard Iannone rich-iannone

View GitHub Profile
@rich-iannone
rich-iannone / new_gt_0.8.0.Rmd
Created December 2, 2022 17:38
This is the slightly revised R Markdown source of the Posit blog post at https://posit.co/blog/new-features-upgrades-in-gt-0-8-0/.
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(gt)
library(gtsummary)
library(tidyverse)
```
@rich-iannone
rich-iannone / gt-color-contrast-computation-comparison.R
Last active October 20, 2023 15:31
This gist generates a gt table that compares text color choices from the WCAG and APCA contrast methods. Implemented in the `data_color()` function of the gt package, where the background is set first (based on cell data) and the color of the overlaid text is determined from the color contrast algorithm.
library(gt)
library(tidyverse)
color_sequence <- seq_along(grDevices::colors())
dplyr::tibble(
x11c = grDevices::colors(),
wcag = color_sequence,
apca = color_sequence
) %>%
@rich-iannone
rich-iannone / DiagrammeR_trigger_script.R
Last active September 20, 2023 06:22
DiagrammeR (https://github.com/rich-iannone/DiagrammeR)— Trigger a script from inside a graph series and modify that graph series. Uses the 'create_series' and 'trigger_script' functions.
# Install the latest version of DiagrammeR from GitHub
devtools::install_github("rich-iannone/DiagrammeR")
# Ensure that the package is loaded
library("DiagrammeR")
# So, here's a script that essentially takes an empty graph series, and
# creates a new graph on each new day it is triggered. It will create
# random nodes each time it's triggered and add those nodes to the graph
# belonging to the current day. Throughout the script, '_SELF_' refers
@rich-iannone
rich-iannone / diagrammer-us-cities.R
Last active September 9, 2023 23:55
Demonstration code for plotting a graph of US cities along with country borders with the DiagrammeR R package.
# Installation
#install.packages("devtools")
devtools::install_github("rich-iannone/DiagrammeR")
library(DiagrammeR)
# Create a graph of border coordinates for USA, Canada, and Mexico
country_graph <- country_graph(iso_a2 = c("US", "CA", "MX"))
# Read in a CSV with US city data
@rich-iannone
rich-iannone / world-cities-diagrammer.R
Created October 5, 2015 07:08
Demonstration code for plotting a graph of world cities along with country borders with the DiagrammeR R package.
# Installation
#install.packages("devtools")
devtools::install_github("rich-iannone/DiagrammeR")
library(DiagrammeR)
# Create a graph of border coordinates
country_graph <- country_graph()
# Read in a CSV with location and population data for cities all over the world
@rich-iannone
rich-iannone / gt_populations.R
Created April 6, 2023 18:31
Population Ranges
library(gt)
library(tidyverse)
countrypops |>
dplyr::filter(year == 2021) |>
dplyr::select(country_code_2, population) |>
dplyr::mutate(population_class = cut(
population,
breaks = scales::breaks_log(n = 20)(population)
)) |>
@rich-iannone
rich-iannone / subset.POSIXct.R
Last active April 8, 2022 08:48
Several examples in R on creating subsets of data via a POSIXct time object.
# Create a simple data frame for testing
df <- data.frame(POSIXtime = seq(as.POSIXct('2013-08-02 12:00'),
as.POSIXct('2013-08-06 05:00'), len = 45),
x = seq(45))
# The Subset Examples
#
# All data on 2013-08-06
sub.1 <- subset(df, format(POSIXtime,'%d')=='06')
@rich-iannone
rich-iannone / news-orgs-early.R
Created April 5, 2022 16:46
An in-progress build of a gt table that uses the `2022-04-05/news_orgs.csv` TidyTuesday dataset
library(gt)
library(tidyverse)
# Table reading
news_orgs <-
readr::read_csv(
file = 'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-04-05/news_orgs.csv',
col_types = cols(
publication_name = col_character(),
parent_publication = col_character(),
@rich-iannone
rich-iannone / gist:1da1ae7a7203958a0c5b1bd1d4b24017
Last active October 13, 2021 03:51
This gt code allows you to make a summary table based on the `pizzaplace` dataset.
library(tidyverse)
library(paletteer)
library(gt)
pizzaplace %>%
mutate(type = case_when(
type == "chicken" ~ "chicken (pizzas with chicken as a major ingredient)",
type == "classic" ~ "classic (classical pizzas)",
type == "supreme" ~ "supreme (pizzas that try a little harder)",
type == "veggie" ~ "veggie (pizzas without any meats whatsoever)",
@rich-iannone
rich-iannone / sf_salaries.R
Last active March 10, 2021 09:47
Plotting SF salaries using the DiagrammeR R package. Dataset is available from Kaggle Datasets.
library(DiagrammeR)
library(DiagrammeRsvg)
library(magrittr)
# The CSV file is located inside a zip file at:
# https://www.kaggle.com/kaggle/sf-salaries/downloads/
# sf-salaries-release-2015-12-21-03-21-32.zip
salaries <- read.csv("Salaries.csv",
stringsAsFactors = FALSE)