Skip to content

Instantly share code, notes, and snippets.

@moodymudskipper
moodymudskipper / find classes
Last active November 6, 2020 04:42
find classes
# run from R GUI or you might have methods registered by IDE
# first method scrape S3 method tables, classes with no methods are not found
# second method parses code to find `class(foo) <- bar` lines and extracts string litterals if found, we could be a bit smarter there
# and find more, but that will still not be exhaustive, because we have things like `class(x) <- cl` and we'd have to check the code
# to see what `cl` is.
# we could also check if some objects are built with `structure`
# calls to `inherits` might also be checked
# The C code should also be inspected or we won't find for instance the "error" or "try-error" classes.
@ikashnitsky
ikashnitsky / pal-safe-five-and-six.R
Last active January 17, 2020 13:43
Safe and efficient palette of five colors for R -- https://twitter.com/ikashnitsky/status/1200570023334559745
#===============================================================================
# 2019-11-30 -- gist
# Safe and efficient palette of five colors, print and colorblind friendly
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com
#===============================================================================
library(magrittr)
library(prismatic)
# a function to test color paletes quickly
library(slide)
library(dplyr, warn.conflicts = FALSE)
# be careful not to `library(tsibble)` as it will overwrite slide()
# quarterly data to start with
df <- tibble(
dates = seq(as.Date("2019-01-01"), as.Date("2021-01-01"), by = "1 quarter"),
yq = tsibble::yearquarter(dates)
)
@emitanaka
emitanaka / collapseoutput.js
Created July 20, 2019 04:43
Collapsible Code Output for `xaringan`
<script>
(function() {
var divHTML = document.querySelectorAll(".details-open");
divHTML.forEach(function (el) {
var preNodes = el.getElementsByTagName("pre");
var outputNode = preNodes[1];
outputNode.outerHTML = "<details open class='output'><summary>Output</summary>" + outputNode.outerHTML + "</details>";
})
})();
(function() {
#===============================================================================
# 2019-07-19-- ikashnitsky.github.io
# Reproduce Figure 2 from http://doi.org/10.1007/s10708-018-9953-5
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com
#===============================================================================
library(tidyverse)
library(hrbrthemes); import_roboto_condensed()
# the data as tribble
@MilesMcBain
MilesMcBain / fools_five.R
Last active July 17, 2019 03:11
Fool's five
f <- function(...) {
function(df) with(df, ...)
}
footate <- function(.data, ...) {
dots <- list(...)
for (column in names(dots)) {
.data[[column]] <- dots[[column]](.data)
}
@benmarwick
benmarwick / prentiss-1998-pca.R
Last active August 13, 2019 00:51
Exploring the PCA published by Prentiss (1998) to understand the usefulness of the Sullivan and Rozen debitage typology
#------------------------------------------------------
# Exploring the PCA published by Prentiss (1998) to understand the
# usefulness of the Sullivan and Rozen debitage typology
# read in & tidy the data -----------------------------------------------
library(tidyverse)
# got these data from table 7 (p. 644) of https://www.jstor.org/stable/2694112
# OCR'd using https://tabula.technology/
prentiss <- readr::read_csv("tabula-Prentiss 1988.csv")
library(ggplot2)

# functions -----------------------------------------------

geom_liquid_area <- function(mapping = NULL, data = NULL, stat = "identity",
                             position = "stack", na.rm = FALSE, show.legend = NA,
                             inherit.aes = TRUE, ...) {
  layer(
    data = data,
library(gganimate) # thomasp85/gganimate
library(cartogram)
library(geogrid) # Need github version jbaileyh/geogrid
library(rnaturalearth)
library(sf)
library(scico)
us <- ne_states('united states of america', returnclass = 'sf')
us <- us[!us$woe_name %in% c('Alaska', 'Hawaii'), ]
us <- st_transform(us, '+proj=eqdc +lat_0=39 +lon_0=-96 +lat_1=33 +lat_2=45 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs')
@dpseidel
dpseidel / default_aes.md
Last active February 12, 2021 17:08
Default ggplot2 aesthetics table

Default ggplot2 aesthetics by geom.

geom alpha angle colour family fill fontface height hjust lineheight linetype shape size stroke vjust weight width xmax xmin ymax ymin
GeomAbline NA black 1 0.5
GeomAnnotationMap NA NA grey20 1 0.5
GeomArea NA NA grey20 1 0.5
GeomBar NA NA grey35 1 0.