Skip to content

Instantly share code, notes, and snippets.

View jimhester's full-sized avatar

Jim Hester jimhester

View GitHub Profile
@jimhester
jimhester / pre-commit
Last active August 29, 2015 14:07
Travis pre-commit hook for R projects
#!/bin/sh
# To use, place this script in your project .git/hooks directory.
# The script will automatically run travis-tool.sh run_tests before committing, so you won't break your project!.
# Modified from http://stackoverflow.com/questions/20479794/how-do-i-properly-git-stash-pop-in-pre-commit-hooks-to-get-a-clean-working-tree
# script to run tests on what is to be committed
# First, stash index and work dir, keeping only the
# to-be-committed changes in the working directory.
old_stash=$(git rev-parse -q --verify refs/stash)
git stash save -q --keep-index
@jimhester
jimhester / .Rbuildignore
Created October 31, 2014 14:05
README.Rmd
^figures/$
^README\.Rmd$
#' ---
#' title: "Notebook Example"
#' author: "Jim Hester"
#' date: "`r Sys.Date()`"
#' output: rmarkdown::html_vignette
#' vignette: >
#' %\VignetteIndexEntry{Notebook example}
#' %\VignetteEngine{knitr::rmarkdown}
#' %\usepackage[utf8]{inputenc}
#' ---
@jimhester
jimhester / error_messages.R
Last active August 29, 2015 14:11
More informative error messages for R
# missing variable
`$` <- function(x, y) { var = deparse(substitute(y)); if(!deparse(substitute(y)) %in% names(x)) { stop('missing variable: ', var) }; x[[var]] }
#> prob1<-as.data.frame(cbind(c(1,2,3),c(5,4,3)))
#> colnames(prob1)<-c("Education","Ethnicity")
#> table(prob1$Eduction, prob1$Ethnicity)
# Error in prob1$Eduction : wrong name: Eduction
# error when nrow is used on non data.frames or matrices
nrow <- function(x) { if (is.data.frame(x) || is.matrix(x)) { dim(x)[1L] } else { stop("nrow cannot be used on ", type(x)) } }
@jimhester
jimhester / linux.txt
Last active August 29, 2015 14:20
Windows LDFLAGS error
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O0 -fprofile-arcs -ftest-coverage -c src/aes.c -o src/aes.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O0 -fprofile-arcs -ftest-coverage -c src/crc32.c -o src/crc32.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O0 -fprofile-arcs -ftest-coverage -c src/digest.c -o src/digest.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O0 -fprofile-arcs -ftest-coverage -c src/init.c -o src/init.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O0 -fprofile-arcs -ftest-coverage -c src/md5.c -o src/md5.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O0 -fprofile-arcs -ftest-coverage -c src/pmurhash.c -o src/pmurhash.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O0 -fprofile-arcs -ftest-coverage -c src/raes.c -o src/raes.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -fpic -g -O0 -fprofile-arcs -ftest-coverage -c src/sha1.c -o src/sha1.o
gcc -std=gnu99
R Under development (unstable) (2015-05-13 r68364) -- "Unsuffered Consequences"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: i386-w64-mingw32/i386 (32-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
@jimhester
jimhester / appveyor.yml
Created June 2, 2015 12:35
Example appveyor YML for Bioconductor package
# DO NOT CHANGE the "init" and "install" sections below
# Download script file from GitHub
init:
ps: |
$ErrorActionPreference = "Stop"
Invoke-WebRequest http://raw.github.com/krlmlr/r-appveyor/master/scripts/appveyor-tool.ps1 -OutFile "..\appveyor-tool.ps1"
Import-Module '..\appveyor-tool.ps1'
install:
library(microbenchmark)
a <- sample(letters, 1e6, TRUE)
b <- as.factor(a)
microbenchmark(c <- a[a == "d"],
d <- b[b == "d"],
e <- {
mtch <- match("d", levels(b))
b[as.integer(b) == mtch]
# benchmarks from http://www.win-vector.com/blog/2015/07/efficient-accumulation-in-r/#more-3295
mkRow <- function(nCol) {
x <- as.list(rnorm(nCol))
# make row mixed types by changing first column to string
x[[1]] <- ifelse(x[[1]]>0,'pos','neg')
names(x) <- paste('x',seq_len(nCol),sep='.')
x
}
library(xml2)
date <- "2015-04-17"
tmp <- tempfile()
system2("svn",
args = c("log",
"-r", sprintf("{%s}:HEAD", date),
"--xml",
"-v"),
stdout = tmp)