Skip to content

Instantly share code, notes, and snippets.

View jimjam-slam's full-sized avatar

James Goldie jimjam-slam

View GitHub Profile
@jimjam-slam
jimjam-slam / ggiraph-flags-fonts.r
Created March 3, 2022 03:11
Combine an interacgive with ggflags and custom fonts with Google Fonts (that work even for remote viewers without the font installed) #rstatstips
library(tidyverse)
library(ggiraph)
library(ggflags)
library(gfonts)
library(here)
mydata <- tribble(
~ x, ~ y, ~ flag, ~ is_home,
1, 1, "au", "yes",
2, 2, "us", "no",
@jimjam-slam
jimjam-slam / factor-values.r
Created February 11, 2022 05:55
Pipe for getting the unique values of every factor column #rstatstips
get_factor_values <- . %>%
summarise(across(
where(is.factor),
list(
n = ~ length(levels(.x)),
# could also pivot longer so that there's a row for each level...
pos_values = ~ paste(levels(.x), collapse = "|")),
.names = "{.col}-{.fn}"
)) %>%
pivot_longer(everything(), names_to = c("col_name", ".value"),
@jimjam-slam
jimjam-slam / r-vscode-keybinds.json
Created February 1, 2022 03:56
Keybindings for writing R in VSCode
[
// open r help
{
"key": "shift+cmd+h",
"command": "r.helpPanel.openForSelection"
},
// insert a magrittr pipe %>% in editor or terminal
{
"key": "shift+cmd+m",
"command": "editor.action.insertSnippet",
@jimjam-slam
jimjam-slam / dockerfile
Last active February 13, 2022 22:55
Sample command line app using rocker #rstatstips
# start with the rocker base
FROM rocker/r-ver:latest
# using littler, install some extra packages
RUN install2.r jsonlite
# add our script
COPY myprogram.r /myprogram.r
# myprogram.r is the entrypoint. when you run the image, ENTRYPOINT + CMD run by default...
@jimjam-slam
jimjam-slam / download-groups.r
Last active February 13, 2022 22:55
Download grouped remote files using friendlier group names #rstatstips
library(tidyverse)
# based on {searchable} and https://stackoverflow.com/a/64931927/3246758
# stats::setNames is fine too!
invert <- function(x) {
set_names(names(x), x)
}
# let's map the coded names to our friendly ones!
fruit <- c(
@jimjam-slam
jimjam-slam / migrate.r
Last active June 10, 2021 02:01
Google Drive folder migration
# this script recursively transfers the contents of a folder to another
# (including a team/shared drive)
# configure it with the ids of your source and target folders below!
# james goldie, june 2021
library(tidyverse)
library(googledrive)
library(collateral)
library(tictoc)
@jimjam-slam
jimjam-slam / ggplot2-gradient-tests.r
Created May 26, 2021 05:44
Ggplot2 gradient tests
library(tidyverse)
my_gradient <- grid::linearGradient(
colours = c("#020024", "#102b81", "#833ab4"),
stops = c(0, 0.4, 1))
# works!
myplot <-
ggplot(mtcars) +
@jimjam-slam
jimjam-slam / side-by-side-ggplot2-title.r
Last active February 13, 2022 22:55
Use duplicate axes (and, here, ggtext for title wrapping) to have a ggplot2 title/subtitle to the side of the plot #rstatstips
library(ggplot2)
library(ggtext)
ggplot(mtcars) +
aes(x = mpg, y = hp) +
geom_point() +
labs(
title = 'This plot has a delightfully long title',
subtitle = 'And a very important message that you should definitely take to heart') +
scale_x_continuous(sec.axis = dup_axis())
@jimjam-slam
jimjam-slam / fetch-error-body.js
Last active May 21, 2020 02:35
Generic fetch handler that throws non-200 responses as objects that include status code, status text and body
// handleJSONRequest: a generic function for ensuring that non-ok (200)
// responses get thrown as errors. use this after fetches and catch with a
// StatusBanner
export function handleJSONRequest(res) {
if (!res.ok) {
return res.text().then(body => {
throw {
status: res.status,
statusText: res.statusText,
message: body
@jimjam-slam
jimjam-slam / parse_time_dimensions.r
Last active February 13, 2022 22:56
Parse a vector of numeric times, given element-wise CF-complaint time dimension units (eg. "days since 1901-01-01") #rstatstips
# https://carbon.now.sh/iYHN1JgadkF76T9rXC7q
# parse_time_dimensions: given a numeric vector of dates or datetimes, and
# a character vector of the form "[units] since [epoch]", return a POSIXct
# vector of date-times that are parsed element-wise. useful if you have,
# say, a list of netcdfs with varied epochs and units.
# example:
# > test %>% mutate(target = parse_time_dimensions(n, t))
# # A tibble: 3 x 3
# n t target
# <int> <chr> <dttm>