Skip to content

Instantly share code, notes, and snippets.

@richfitz
richfitz / functions.R
Last active May 24, 2021 14:34
mclapply -> rrqlapply
use <- function(x, default){
if(is.null(x))
default
else
x
}
sim_fkf <- function(fit){
n <- fit[["n"]]
dt <- fit[["dt"]]
@richfitz
richfitz / gist:f949282560ee78c581c3
Last active August 29, 2015 14:16
libjq / R musings

Installation on OSX is currently a PITA if you don't have brew (I don't so that my system stays more similar to users of things, and because I'm a masochist apparently). See this issue as this will be fixed in jq 1.5. So, create an executable shell script sheep in the jq directory containing

#!/bin/sh
echo "baa"

and run YACC=./sheep ./configure - make should run fine after that.

There is a library target for jq, which is great because now we only have to wrap a library, rather than write one. Further, there is a set of Python bindings that we can use as a starting point. The repo is here.

Ruby bindings here

With `CXX_STD = CXX11` set in src/Makevars, I get:
```
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.1/Resources/library’
* installing *source* package ‘RcppAnnoy’ ...
** libs
I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I../inst/include/ -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/BH/include" -c annoy.cpp -o annoy.o
/bin/sh: I/Library/Frameworks/R.framework/Resources/include: No such file or directory
make: [annoy.o] Error 127 (ignored)
-dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -L/usr/local/lib -o RcppAnnoy.so annoy.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
## Empty list -- just use the empty environment for this.
nil <- function() {
emptyenv()
}
## Test if a list is the empty list:
is_empty <- function(lis) {
identical(lis, nil())
}
git init
# Create a big file of random data (50 MB)
dd if=/dev/random of=bigblob.dat bs=5000000 count=10
git add bigblob.dat
git commit -m "Add big binary"
du -sh .git # about 48M
# Should be one big file in here:
@richfitz
richfitz / details.md
Last active September 16, 2020 10:28
Unify (and unixify) line endings through a git repository's history

Basic strategy same as here but having some trouble with getting find to behave like the article, and working around files that have spaces or other special character in them (especially apostrophes, as they are in people's names).

The fix-eol.sh file is basically the same as the articles, except that we use a for loop over xargs so that the name escaping can be done.

The fix-eol-1.sh file converts all line endings to Unix for a single file. The issues are mostly Mac, but this does Windows -> Unix first so that the \r in Windows files isn't also changed to a \n (giving \n\n).

Run by doing

git filter-branch --tree-filter '~/Documents/Projects/baad/fix-eol.sh' --prune-empty -- --all
library(testthat)
nr <- 5
grid <- matrix(seq_len(nr^2), nr, nr, byrow=TRUE)
data <- data.frame(grid=seq_len(nr^2),
sp1=runif(nr^2) < .3,
sp2=runif(nr^2) < .5,
sp3=runif(nr^2) < .7,
stringsAsFactors=FALSE)
@richfitz
richfitz / asr.R
Created August 16, 2013 03:25
Quick and dirty ancestral state reconstruction and change counting
# There are a number of problems here:
# 1. I don't see how to convert from states above 50% probability to
# number of events
# 2. What ace returns are conditional likelihoods, not the marginal
# likelihoods (which is what you need for ASR). The conditional
# likelihood is just the probability of the data descended from that
# node, not the probability that the node is in that state, taking
# into account the *other* nodes in the tree. I've not managed to
@richfitz
richfitz / label.R
Last active May 24, 2021 14:30
Plotting worked example
## Position label at a fractional x/y position on a plot
label <- function(px, py, lab, ..., adj=c(0, 1)) {
usr <- par("usr")
text(usr[1] + px*(usr[2] - usr[1]),
usr[3] + py*(usr[4] - usr[3]),
lab, adj=adj, ...)
}
@richfitz
richfitz / weather.R
Created June 17, 2013 06:47
Download some weather data from openweathermap.org
library(RCurl)
library(rjson)
url.base <- function()
"http://api.openweathermap.org/data"
city.to.id <- function(city, country="AU") {
url <- sprintf("%s/2.5/find?q=%s", url.base(), city)
res <- fromJSON(getURLContent(url))
if (!is.null(country)) {