Skip to content

Instantly share code, notes, and snippets.

View leeper's full-sized avatar

Thomas J. Leeper leeper

View GitHub Profile
@leeper
leeper / slopegraph.r
Last active December 26, 2015 13:29
Edward Tufte-style Slopegraphs in R. Find the current version of this code here: https://github.com/leeper/slopegraph
# R Function to Draw Edward Tufte-style Slopeplots
slopegraph <-
function(
df,
xlim = c(.5,ncol(df)+.5),
ylim = c(min(df)-diff(range(df))/100,max(df)+diff(range(df))/100),
main = NULL,
bty = 'n',
yaxt = 'n',
@leeper
leeper / dropshadow.r
Created November 4, 2013 18:13
Test of drop shadows on white and black backdrops
# Drop shadows
# some data
x <- cbind(c(2,3,3,4,5,3,2),
c(3,3,5,4,3,2,1),
c(1,2,1,2,2,3,6))
# Dark backdrop
# set plot background to black
par(bg='black', col.axis='white', col.lab='white',
@leeper
leeper / sweave-executable.r
Created November 6, 2013 10:35
R script for Windows, which can be run to fully compile the latest updated .rnw file in a directory.
# Sweave Compilation Shell Script
# Thomas J. Leeper
# Created 2011-08-12
# Updated 2012-08-29
# Copyright (C) 2012 Thomas J. Leeper
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
sink("execute.log",split=FALSE,type = c("output", "message")) #Optional save shell script output
@leeper
leeper / gender.R
Last active October 11, 2017 18:37
Gender API Example https://gender-api.com/en/
#' Gets gender by name or email address, optionally by country or IP address.
library("httr")
library("XML")
#' @import httr
#' @import rjson
#' @param name A character string containing a first name, or a character vector containing first names. One must specify name or email.
#' @param email A character string containing an email address with a first name. One must specify name or email.
#' @param country An optional character string containing a two-letter country name, as listed here: https://gender-api.com/en/api-docs
@leeper
leeper / plotthemes.R
Last active October 28, 2016 15:23
Experimenting with themes in base graphics
#' The goals here are to:
#' (1) play around with themes to make base graphics less ugly
#' (2) atomically specify multiple plot aspects (data, axes, grid, title) in a single call
#' (3) Recycle last plot theme to easily duplicate its appearance in the next plot
#' The idea is to create a set of customizable functions like `theme()`, which return a list of graphics arguments
#' These can then be passed to `splot`, which will cleanly handle them by passing some to `par` and some to `plot`
#' Different `theme()` functions can then be used to modify color palettes, axes, etc.
merge.list <-
function (x, y) {
@leeper
leeper / catfacts.R
Created February 19, 2014 20:25
Get specified number of cat facts from the Cat Facts API (http://catfacts-api.appspot.com/)
library("RCurl")
library("RJSONIO")
catfacts <- function(number=1){
if(is.null(number) || is.na(as.numeric(number)) || number < 1)
stop("'number' must be a positive integer")
fromJSON(getURL(paste('http://catfacts-api.appspot.com/api/facts?number=', number, sep='')))$facts
}
@leeper
leeper / update_github.R
Last active February 2, 2021 22:56
Update packages if a newer version is available from GitHub
library('devtools')
library('utils')
library('httr')
update_github <-
function(ask = TRUE, ...){
installed <- installed.packages()
oldVersion <- installed[,'Version']
urls <- sapply(names(oldVersion), function(x){
d <- packageDescription(x)
@leeper
leeper / lookfor.R
Last active August 29, 2015 14:00
Search for names, labels, and levels in R
# This is an effort to emulate Stata's `lookfor` in R
lookfor <- function(what, ls_opts = list(), ...){
s <- do.call("ls", ls_opts)
d <- lapply(s, lookin, what = what, ...)
# return value should be a list with the string matching the search, along with details of its position
# big challenge is doing this recursively because, e.g., lists of lists of dataframes would be really difficult to search
class(d) <- 'lookfor'
@leeper
leeper / federalregister.R
Last active August 29, 2015 14:01
Initial ideas for Federal Register API client
# https://www.federalregister.gov/developers/api/v1
# NAMESPACE
# importFrom(RJSONIO, fromJSON)
# importFrom(RCurl, curlPerform, basicTextGatherer)
# S3method(print,fedreg_document)
# S3method(print,fedreg_agency)
fr_search <- function(version='v1', ...) {
@leeper
leeper / readwrite.R
Last active August 29, 2015 14:01
Outline of a more convenient i/o function for R
# call package easyio
library('foreign')
library('tools')
# library('haven') # for spss (sav, por), stata (dta), sas
#library('memisc') # for `spss.portable.file` and `spss.system.file`
#library('hmisc') # for `spss.get`
#library('openxlsx') # for `read.xlsx`
#library('data.table') # for fread