Skip to content

Instantly share code, notes, and snippets.

View leeper's full-sized avatar

Thomas J. Leeper leeper

View GitHub Profile
@leeper
leeper / pkg_names.R
Last active December 27, 2015 16:24
How much of the R package namespace is left?
# R only has one package namespace
# All packages exist in a single, non-hierarchical namespace
# Once a package name is used, it cannot be reused
# CRAN hosts most existing R packages; Bioconductor holds many more; once claimed on either service, a package name is reserved
# GitHub hosts many other packages with no "claim" to a given package name and there's no list of such packages
#
# "Writing R Extensions" says this about names:
## "The mandatory ‘Package’ field gives the name of the package.
## This should contain only (ASCII) letters, numbers and dot, have
## at least two characters and start with a letter and not end in
@leeper
leeper / pkg_age.R
Last active January 9, 2016 20:37
CRAN Package Age
library("httr")
library("XML")
x <- content(httr::GET("https://cran.r-project.org/web/packages/available_packages_by_date.html"), as = "text")
p <- readHTMLTable(x, stringsAsFactors = FALSE)[[1]]
names(p) <- trimws(names(p))
p$Days <- Sys.Date() - as.Date(p$Date)
h <- hist(-as.numeric(p$Days), breaks = seq(from = -(365*11), to = 0, by = 365/4), plot = FALSE)
barplot(h$counts, space = 0, las = 1,
@leeper
leeper / merge_list.R
Last active March 4, 2017 01:19
Merge list of data.frames
# packages
library("dplyr")
library("reshape")
library("microbenchmark")
set.seed(1)
# prep data
m <- mtcars
n <- 50L # number of data.frames
@leeper
leeper / ShinyPerspectivePlot.R
Last active November 9, 2021 10:26
Shiny App for 3D Interaction plots
library("shiny")
library("shinyjs")
ui <- shinyUI(pageWithSidebar(
titlePanel("", "3D Perspective Plot for Interaction Effects"),
sidebarPanel(
tabsetPanel(
tabPanel("Data",
@leeper
leeper / recoding.R
Last active April 3, 2017 12:52
Some notes on recoding
# recoding is basically a function
# x is a variable
# f() is a function
# f(x) is a recoding operation
#
# implementation
# if x is discrete, then f() is simple to implement as a map between input values and output values:
# 1 = 0
# 2 = 1
#
@leeper
leeper / assert.R
Created March 1, 2016 15:41
Assertion framework for checking recodings
# relevant to recoding: https://gist.github.com/leeper/c669fd4a6dcb9ac23612
# assertion function
assert <- function(x, length, class, min, max, has, excludes, of, miss) {
if (!missing(length)) {
stopifnot(length(x) == length)
}
if (!missing(class)) {
stopifnot(inherits(x, as.character(substitute(class))))
}
@leeper
leeper / charsub.R
Created March 10, 2016 11:05
Positional character substitution
charsub <- function(x, i, value) {
out <- lapply(strsplit(x, ""), function(z) {
z <- `[<-`(z, i, value)
z[is.na(z)] <- " "
paste0(z, collapse = "")
})
unlist(out)
}
# SINGLE STRING
@leeper
leeper / explot.R
Last active March 12, 2016 13:40
Playing with plotting difference-of-means tests
set.seed(1)
n <- 300
x <- rbinom(n, 1, .5)
y <- 4 + (2 * x) + rnorm(n, 0, 3)
m <- tapply(y, x, mean)
wm <- weighted.mean(c(mean(y[x==1]), mean(y[x==0])), c(length(y[x==1]), length(y[x==0])))
ci <- function(formula, conf.level, var.equal = FALSE) {
(tt <- t.test(formula, var.equal = var.equal, conf.level = conf.level))
rect(0, wm - diff(tt$conf.int)/2,
@leeper
leeper / checkurls.R
Last active April 30, 2016 06:36
Check URLs in a document
# Check URLs in a document
## This code will extract URLs from a text document using regex,
## then execute an HTTP HEAD request on each and report whether
## the request failed, whether a redirect occurred, etc. It might
## be useful for cleaning up linkrot.
if (!require("httr")) {
install.packages("httr", repos = "http://cran.rstudio.com/")
}
@leeper
leeper / jsevents.html
Last active May 16, 2016 08:24
Javascript event tracking
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<form>
<div>
<p>Mouse: <input type="text" id="xmove" />
<input type="text" id="ymove" />