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
#' Apply a function over groups of columns.
grouped_columns_apply = function (data, width, fun) {
# Generate the group indices for the columns (see below).
group_indices = group_indices(ncol(data), width)
# For each group, apply `fun` to the columns of its indices.
results = lapply(group_indices, function (indices) fun(data[, indices, drop = FALSE]))
bind_cols(results)
}
# If the number of columns is always evenly divisible by the width, the following
@klmr
klmr / # glibc - 2017-10-03_18-51-18.txt
Created October 3, 2017 17:59
glibc on Ubuntu 12.04.5 LTS - Homebrew build logs
Homebrew build logs for glibc on Ubuntu 12.04.5 LTS
Build date: 2017-10-03 18:51:18
@klmr
klmr / select-lines.cpp
Created June 5, 2017 12:40
Efficiently subset many lines from a text file
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
auto main(int argc, char** argv) -> int {
if (argc < 2) {
context('Same as the basic tests, just in an interactive R shell')
play_back_results = function (record_events, onto) {
for (event in record_events) {
switch(event$type,
start_test = onto$start_test(event$context, event$test),
add_result = onto$add_result(event$context, event$test, event$result),
end_test = onto$end_test(event$context, event$test))
}
}
@klmr
klmr / README.md
Last active November 14, 2024 11:42
Debug which functions access .Random.seed

Who is touching the .Random.seed?

Inspired by a Stack Overflow question, here’s a way of tracking what’s been modifying the .Random.seed.

Since R makes static analysis impossible in general, the following is a runtime tracer that injects itself into the .Random.seed variable via an active binding:

NOTE: This will only work properly if called directly by the user at the top level. In particular, it won’t work inside an RMarkdown report or similar, where the top-level call is rmarkdown::render(), or knitr::knit(), or similar; rather than, say, sample().

trace_random_seed()
@klmr
klmr / remove-mate.pl
Created April 15, 2017 13:22
Remove the read mate number from the end of read IDs in a Fastq file
#!/usr/bin/env perl
use strict;
use warnings;
my $state = 0; # 0 = ID; 1 = sequence; 2 = repeat ID; 3 = quality
my $seqlen = 0;
while (<>) {
chomp;
@klmr
klmr / r-launcher.sh
Last active March 31, 2017 16:02
R launcher for Nvim-R on an LSF cluster
#!/usr/bin/env bash
while getopts ":n:c:q:" option; do
case "$option" in
n)
cores="$OPTARG"
;;
m)
mem="$OPTARG"
;;
@klmr
klmr / pillows.r
Created February 23, 2017 12:20
Ordering pillows online
library(dplyr)
library(tidyr)
library(ggplot2)
stages = c('Ordered', '?', 'In transit', 'Arrived', 'Returned')
make_stages = function (status)
factor(status, levels = stages, ordered = TRUE)
pillows = tibble::tribble(
@klmr
klmr / # util-linux - 2017-02-14_14-50-53.txt
Created February 14, 2017 14:55
util-linux on Ubuntu 12.04.5 LTS - Homebrew build logs
Homebrew build logs for util-linux on Ubuntu 12.04.5 LTS
Build date: 2017-02-14 14:50:53
@klmr
klmr / # msgpack - 2017-02-14_14-29-12.txt
Created February 14, 2017 14:35
msgpack on Ubuntu 12.04.5 LTS - Homebrew build logs
Homebrew build logs for msgpack on Ubuntu 12.04.5 LTS
Build date: 2017-02-14 14:29:12