Skip to content

Instantly share code, notes, and snippets.

View klmr's full-sized avatar
📦
Making your R code easier to reuse

Konrad Rudolph klmr

📦
Making your R code easier to reuse
View GitHub Profile
@klmr
klmr / valplot.rmd
Last active August 29, 2015 14:06
Create a valplot in R
Create two bimodal toy distributions.
```{r}
a = c(rnorm(100, 5, 2), rnorm(100, 15, 3))
b = c(rnorm(100, 3, 3), rnorm(100, 14, 1))
```
Set up the graphics environment.
We draw a density curve. `polygon` draws a filled curve, but you don’t seem to be able to control the border width.
Tile LoadFromFile(std::string filename) {
std::ifstream ifs{filename};
std::string line;
if (not getline(ifs, line))
throw "foo";
std::string name{line};
int numBoxes{};
int numTargets{};
@klmr
klmr / settings.py
Created August 7, 2014 09:30
Grip settings file for OS X, which retrieves the GitHub password from “Keychain Access.app”
def find_password():
import subprocess
import re
cmd = ['security', 'find-internet-password', '-gs', 'github.com']
pwinfo = subprocess.Popen(cmd, stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
pwline = pwinfo.stderr.read().strip()
return re.sub('password: "(.*)"', '\\1', pwline)
@klmr
klmr / lambda.r
Last active April 13, 2023 11:27
Finally a proper lambda for R
`<-` = function (body, params) {
vars = all.vars(substitute(params))
formals = as.pairlist(setNames(replicate(length(vars), quote(expr = )), vars))
eval.parent(call('function', formals, substitute(body)))
}
sapply(1 : 4, x -> 2 * x)
# 2 4 6 8
mapply(x ~ y -> x + y,
@klmr
klmr / xrange.r
Last active August 29, 2015 14:01
Python-like range operator in R, allowing to specify step size
`:` = function (a, b) {
if (inherits(a, 'xrange'))
do.call(seq, as.list(c(range(a), by = b)))
else if (inherits(a, 'factor'))
interaction(a, b, sep = ':')
else
structure(seq(a, b), class = 'xrange')
}
print.xrange = function (x)
In [4]: x = [ ['a'], ['b'], ['c'] ]
In [5]: for i in x:
...: print id(i)
...: i[0] = 'x'
...: print id(i)
...:
4366314384
4366314384
4370788648
auto&& hello = command<int, std::string>{{
option{"--count", 1, "number of greetings"},
option{"--name", "", "the person to greet", flags::required}},
[](int count, std::string name) {
for (auto i : range(0, count))
std::cout << "Hello " << name << '\n';
}};
int main(int argc, char* argv[]) {
return hello(argc, argv);
@klmr
klmr / gist:11235107
Last active August 29, 2015 14:00
Rename full reference in BAM file
mv "$input" "$input.tmp"
samtools view -h "$input.tmp" \
| awk -F '\t' -v OFS='\t' '
/^@SQ/ {
$2 = "SN:chr" substr($2, 4, match($2, " ") - 4)
print
}
/^@/ && !/^@SQ/ { print }
@klmr
klmr / arrays.java
Created April 15, 2014 09:43
Binary search implementation for a primitive type in Java.
static int binarySearch(int[] arr, int key) {
Object[] arrobj = new Object[arr.length];
for (int i = 0; i < arr.length; i++)
arrobj[i] = arr[i];
Object keyobj = key;
return binarySearch(arrobj, keyobj);
}
@klmr
klmr / repro.r
Created April 7, 2014 12:44
Reproducible name clash with R packages
# Reproduce a name clash with packages
# R 3.0.2
library(DESeq) # 1.14.0
x = do.call(rbind, rep(list(read.table(text='1 2\n3 4')), 10))
m = c('V1', 'V2')
cds = estimateSizeFactors(newCountDataSet(x, m))
cds1 = estimateDispersions(cds, method='blind', fitType='local')
lp = function () 42
cds2 = estimateDispersions(cds, method='blind', fitType='local')