Skip to content

Instantly share code, notes, and snippets.

View jimjam-slam's full-sized avatar
📊
Building data vis!

James Goldie jimjam-slam

📊
Building data vis!
View GitHub Profile
@jimjam-slam
jimjam-slam / script-time.r
Last active February 13, 2022 23:00
Returns the time elapsed since the script was sourced. Great for profiling! Source it at the start of your script, then call run.time() when outputting debug info #rstatstips
# james goldie, climate change research centre, unsw, dec 2015
# run.time: returns the time elapsed since this script was
# sourced in sensible units as a string ('**h**m**s')
script.start.time = Sys.time()
run.time <- function()
{
now = Sys.time()
return(paste0(
as.integer(difftime(now, script.start.time, units = 'hours')),
@jimjam-slam
jimjam-slam / count-leap-days.r
Last active February 13, 2022 23:00
Get the number of leap days between a vector of dates and a constant epoch. Useful if you need to go between Gregorian and Julian/365 day calendars #rstatstips
# count_leap_days: returns an integer for the number of leap days between a
# vector of dates and a constant epoch
count_leap_days <- function(dates, epoch = ymd('1850-01-01'), proleptic = FALSE)
{
require(lubridate)
# check input
if (!is(epoch, 'Date') | !is(dates, 'Date'))
{
stop('count_leap_days: both arguments must be Date objects.')

Keybase proof

I hereby claim:

  • I am jimjam-slam on github.
  • I am rensa (https://keybase.io/rensa) on keybase.
  • I have a public key whose fingerprint is 0A48 7DAE 8103 D2B4 6C50 1815 AF35 3FD1 0B42 DABB

To claim this, I am signing this object:

@jimjam-slam
jimjam-slam / iso_to_emoji.r
Last active August 31, 2017 05:57
Convert ISO 3166-1 alpha-2 country codes to their corresponding emoji flag control sequences (either as Unicode characters or ASCII strings). Great for use with the countrycode, emojifont and emoGG packages! Requires stringr (part of the tidyverse).
# these two functions, iso_to_emoji_unicode and iso_to_emoji_ascii, convert two-letter iso country codes
# (eg. australia = AU) to the unicode control sequences used for their flag emoji. the _unicode function
# outputs the actual unicode characters (printed in R in ascii but stored in ascii); the _ascii function
# outputs an ascii representation (with the argument ligature_sep in between the two strings).
library(stringr)
# iso_to_emoji_unicode: for a vector of two-letter iso codes, returns a
# corresponding vector of two-character unicode control sequences.
# (nb: R prints them in ascii, but they're really stored as unicode characters)
# great for use with countrycode and emojifont!
@jimjam-slam
jimjam-slam / add-flag-borders.r
Last active September 1, 2017 10:50
Add a circular border to each of the circular EmojiOne flags. Useful snippet for the next time I need to use the XML package in about forty years.
library(XML)
for (file in list.files(pattern = glob2rx('*.svg')))
{
message('Processing ', file)
flag = xmlParse(file)
circle_node = newXMLNode('circle',
attrs = c(
'fill' = 'none',
'stroke' = '#333',
@jimjam-slam
jimjam-slam / pub-list.html
Last active September 21, 2017 06:55
Add an academic publication list to your academic blog.
<div class="publist">
<ul>
{% for post in site.categories.science %}
{% if post.work-type == 'Paper' %}
<li>
<a href="{% if post.ref-doi %}http://dx.doi.org/{{ post.ref-doi }}{% else %}{{ post.url | prepend: site.baseurl }}{% endif %}"><h2>{{ post.ref-authors }} ({{ post.ref-year }}).</h2></a>
<p>{{ post.ref-title }}. <em>{{ post.ref-journal}}</em>{% if post.ref-vol %}, {{ post.ref-vol }}{% endif %}. <a href="http://dx.doi.org/{{ post.ref-doi }}">doi: {{ post.ref-doi }}</a></p>
</li>
{% endif %}
{% endfor %}
@jimjam-slam
jimjam-slam / tasks.json
Last active July 20, 2018 05:44
Task runner to render bookdown projects in Visual Studio Code
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Knit bookdown project",
"type": "shell",
"command": "/usr/local/bin/RScript -e 'setwd(\"index\"); bookdown::render_book(\"index.rmd\"); setwd(\"..\");'",
"windows": {
@jimjam-slam
jimjam-slam / rmd-snippets.json
Created February 22, 2018 23:15
Visual Studio Code snippets for working with RMarkdown files. Go to Configure User Snippets and drop it in.
{
// Snippets for talking to yourself
"TK (citation needed)": {
"prefix": "tkcite",
"body": "_(**TKTK** - citation needed)_",
"description": "Add a note to yourself to cite this later."
},
"TK": {
"prefix": "tktk",
"body": "_(**TKTK** - blah blah blah)_",
@jimjam-slam
jimjam-slam / tips-highlight-features.r
Last active February 13, 2022 22:58
Use case_when with geom_text to automatically label points that mmeet certain conditions (extrema, significance, etc.) #rstatstips
library(tidyverse)
library(ggrepel) # substitude geom_text for geom_text_repel if you don't want to bother with this!
mydata =
data_frame(
x = 1:10,
y = rnorm(10),
name = c('Apple', 'Banana', 'Kiwi', 'Orange', 'Watermelon',
'Grapes', 'Pear', 'Cantelope', 'Tomato', 'Satsauma')) %>%
mutate(
@jimjam-slam
jimjam-slam / gridsvg-example-barchart.r
Last active January 19, 2024 15:00
How to add SVG attributes to a ggplot to assist in building interactives for the web #rstatstips
# gridSVG::grid.garnish can be used to add SVG attributes to elements of a plot!
# we don't even need to do it one at a time: with the option `group = FALSE`, we
# can give it a vector of values for an attribute and garnish an entire geom at
# once. the only rub is that sometimes ggplot2 shuffles your data around when
# plotting it. we need to check how the data was reordered, and arrange our
# original data to match, before we send it to grid.garnish.
library(tidyverse)
library(magrittr)
library(grid)