Skip to content

Instantly share code, notes, and snippets.

@richfitz
richfitz / gist:2324707
Last active August 27, 2019 11:36
Install MinionPro on OSX
#!/bin/sh
## Information
## http://carlo-hamalainen.net/blog/2007/12/11/installing-minion-pro-fonts/
## http://www.ctan.org/tex-archive/fonts/mnsymbol/
## 0: Install
## http://www.lcdf.org/type/
## I used --without-kpathsea to configure (with MacTeX 2011):
##
## ./configure --without-kpathsea
@richfitz
richfitz / Rd2Knitr2HTML.R
Created May 10, 2012 21:36
Convert Rd files to HTML, knit()'ing examples
#!/usr/bin/Rscript
library(tools)
library(knitr)
library(sowsear)
opts_knit$set(progress = FALSE, verbose = FALSE)
## We need a list of files and a package to start.
args <- commandArgs(TRUE)
package <- args[[1]]
files <- args[-1]

Suppose you have a long running calculation:

f <- function(x) {
  message("Evaluating slow function")
  Sys.sleep(5) # sleep 5 seconds to simulate long running time
  x
}
## Follow steps in original post to download files using ghrabber.
## https://github.com/trestletech/rhistory/
## Then load as one big character vector (161828 entries on 21 Feb 2013).
dat <- unlist(lapply(dir("fetched", full=TRUE), readLines))
## Count uses of TRUE / FALSE
length(grep("\\b(TRUE|FALSE)\\b", dat)) # 4389
## and of T / F shortcuts
length(dat[grep("\\b[TF]\\b", dat)]) # 2432
@richfitz
richfitz / gist:5056365
Created February 28, 2013 12:21
Negating tests with testthat
library(testthat)
## This works but won't correct labels:
not <- function(f) {
function(...) {
res <- f(...)
res$passed <- !res$passed
res
}
}
@richfitz
richfitz / gist:5258213
Last active December 15, 2015 12:09
Stargazer, knitr & R markdown
Stargazer and R Markdown to LaTeX
========================================================
If you're using LaTeX, then use the Sweave-like knitr opions here instead. The `results='asis'` means that knitr won't do any processing on the output, so when you run this it produces a bunch of gobbledygook.
```{r}
library(stargazer)
```
@richfitz
richfitz / gist:5297666
Created April 3, 2013 01:08
Multi panel figure with inset plots in base graphics
## OK, this turned out to be more complicated than I would have
## thought. I feel that this approach *should* work, but it confuses
## R's multi-panel plots:
## Smaller margins, plus a top margin
par(mar=c(3.1, 3.1, .5, .5), oma=c(0, 0, 1.5, 0), mfrow=c(2, 2))
## Plot dummy data
plot(1:10)
@richfitz
richfitz / gist:5431394
Created April 21, 2013 22:50
A somewhat sensible use of <<-
## Captures a variable 'n' in the local scope of the returned
## function. When you run that function it increments the counter and
## prints its current value.
make.counter <- function(start=0) {
n <- start
function() {
n <<- n + 1
message(sprintf("counter value is %d", n))
}
}
@richfitz
richfitz / or.cpp
Last active December 16, 2015 12:19
Faster version of 1-(prod(1-x))
#include <vector>
// [[Rcpp::export]]
double or_cpp(std::vector<double> x) {
double tot = 1.0;
for ( size_t i = 0; i < x.size(); i++ )
tot *= (1 - x[i]);
return 1 - tot;
}
/*
@richfitz
richfitz / gist:5609592
Created May 19, 2013 23:42
Export issues from Github as JSON
"""
Exports Issues from a specified repository.
Uses basic authentication (Github username + password) to retrieve Issues
from a repository that username has access to. Supports Github API v3.
From:
https://gist.github.com/unbracketed/3380407
"""
import requests