Skip to content

Instantly share code, notes, and snippets.

View leeper's full-sized avatar

Thomas J. Leeper leeper

View GitHub Profile
@leeper
leeper / TESS.r
Created May 15, 2014 22:18
TESS Data Archive Scraper
require('XML')
tess_studies_list <- function(){
x <- xmlParse('http://www.tessexperiments.org/previousstudies.html')
}
get_tess_study <- function(id, file){
download.file()
}
@leeper
leeper / google_dmca.R
Last active February 18, 2020 17:51
Quick analysis of Google's DMCA Data
# Google DMCA Transparency Data
# 2014-07-24
# https://www.google.com/transparencyreport/removals/copyright/data/
# get the data
download.file('http://transparencyreport.storage.googleapis.com/google-websearch-copyright-removals.zip',
'localcopy.zip', method = 'curl')
unzip('localcopy.zip', files = 'google-websearch-copyright-removals/requests.csv')
@leeper
leeper / by.R
Last active August 29, 2015 14:05
Creating a simple by pipe operator
# function definition
`%by%` <- function(a,b){
s <- substitute(a)
l <- as.list(s)
p <- parse(text=s)
if(length(p)>2) {
l <- l[3:length(l)]
do.call("by", c(list(data=eval.parent(p[2]),
INDICES=b,
FUN=eval.parent(p[1])),
@leeper
leeper / winbuild.txt
Last active July 25, 2021 02:36
Building R from source on Windows
# Outline for building R from source on Windows (7)
# http://cran.r-project.org/doc/manuals/r-release/R-admin.html#Building-from-source
# Pre-built Windows R-Devel binary: http://cran.r-project.org/bin/windows/base/rdevel.html
# possibly helpful: http://stackoverflow.com/questions/25451705/build-r-source-code-from-windows
# install Rtools
# http://cran.r-project.org/bin/windows/Rtools/
# install Inno Setup (Unicode version)
@leeper
leeper / pushpop.R
Last active July 19, 2024 15:09
One-line push and pop in R
# push
push <- function(x, values) (assign(as.character(substitute(x)), c(x, values), parent.frame()))
# pop
pop <- function(x) (assign(as.character(substitute(x)), x[-length(x)], parent.frame()))
# example
z <- 1:3
push(z, 4)
z
@leeper
leeper / ReversePolish.R
Last active August 29, 2015 14:15
Reverse Polish
rp <- function() {
stack <- numeric()
push <- function(values) stack <<- c(stack, values)
pop <- function() {
a <- stack[length(stack)]
stack <<- stack[-length(stack)]
return(a)
}
reverse <- function() {
r <- readline()
@leeper
leeper / gettext_workflow.txt
Last active August 29, 2015 14:17
Package message translations
1. Install GNU gettext (on Windows, this can be obtained from the Rtools site: http://www.stats.ox.ac.uk/pub/Rtools/goodies/gettext-tools.zip)
2. Run `update_pkg_po()` (https://stat.ethz.ch/R-manual/R-devel/library/tools/html/update_pkg_po.html)
a. Create `/po` if it does not exist
b. Call `xgettext2pot()` to create/update `po/R-pkgname.pot` file
c. All `R-lang.po` files in `/po` are updated from `po/R-pkgname.pot` using `msgmerge`
d. `checkPoFiles()` is called on updated files
e. If check is successful, messages are compiled using `msgfmt` system call and installed in `/inst/po`.
f. In a UTF-8 locale, a ‘translation’ ‘[email protected]’ is created with UTF-8 directional quotes, compiled and installed under ‘inst/po’.
g. If `po/pkg.pot` exists:
i. `/src` is examined to create `/po/pkg.pot`
#' Parse a codebook file with variable and level information.
#'
#' Parses a codebook file where lines starting at column zero (far left) represet
#' variable information (e.g. name, description, type) and indented lines
#' (i.e. lines beginning with white space, either tabs or spaces, etc.) represent factor
#' levels and labels.
#'
#' Note that white space at the beginning and end of each line is stripped before
#' processing that line.
#'
@leeper
leeper / code.R
Last active August 29, 2015 14:17
Reading fixed width format data
read.fwf2 <- function (file, widths, header = FALSE, sep = "\t", skip = 0,
row.names, col.names, n = -1, ...)
{
doone <- function(x) {
x <- substring(x, first, last)
x[!nzchar(x)] <- NA_character_
paste0(x, collapse = sep)
}
if (is.list(widths)) {
recordlength <- length(widths)
@leeper
leeper / read.qualtrics.csv.R
Created April 13, 2015 10:54
Function read data from a Qualtrics-generated CSV file
read.qualtrics.csv <- function(filename, stringsAsFactors = FALSE, ...) {
n <- read.csv(filename, nrows = 1, stringsAsFactors = FALSE)
dat <- read.csv(filename, header = FALSE, skip = 2, stringsAsFactors = stringsAsFactors, ...)
names(dat) <- names(n)
names(dat)[1:10] <- n[1,1:10]
for(i in seq_along(dat)) {
attr(dat[,i], "question") <- n[1,i]
}
dat
}