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 / sys.r
Created February 18, 2015 17:04
A command line script wrapper module for R
# Command line tools don’t want to clutter their output with unnecessary noise.
library = function (...)
suppressMessages(base::library(...))
#' The command line arguments
args = commandArgs(trailingOnly = TRUE)
#' The name of the script
#'
#' @note If the script was invoked interactively, this is the empty string.
@klmr
klmr / tpm.r
Last active August 29, 2015 14:15
library(dplyr)
counts = data.frame(Gene = c('A', 'B', 'C', 'D'),
Length = 1:4,
do1 = c(10, 20, 30, 40),
do2 = c(20, 40, 60, 80),
do3 = c(20, 20, 30, 40))
tpm = counts %>%
mutate_each(funs(. / Length), starts_with('do')) %>%
@klmr
klmr / Numbers.java
Last active August 29, 2015 14:14
Parsing and writing numbers in different bases
package demo;
public class Numbers {
static final String hexDigits = "0123456789ABCDEF";
static final String decDigits = "0123456789";
static int fromBaseRepresentation(final String value, final String digits) {
final int base = digits.length();
int result = 0;
for (final char c : value.toCharArray()) {
@klmr
klmr / tagviews.r
Created January 30, 2015 14:13
Create top 20 list of your Stack Overflow tag views
#!/usr/bin/env Rscript
options(stringsAsFactors = FALSE)
library(dplyr)
library(ggplot2)
library(jsonlite)
# Usage:
# ./tagviews.r datafile.json output.png
args = commandArgs(TRUE)

Keybase proof

I hereby claim:

  • I am klmr on github.
  • I am klmr (https://keybase.io/klmr) on keybase.
  • I have a public key whose fingerprint is C50E C91D 5CB6 5576 F218 02A7 819B 7ADF DFF6 82BE

To claim this, I am signing this object:

@klmr
klmr / hohoho.r
Created December 23, 2014 17:17
Merry χmas, deaR people
`^` = function (a, b) UseMethod('^')
`^.default` = .Primitive('^')
`^.character` = function (str, n) paste(rep(str, n), collapse = '')
'ho' ^ 3
# "hohoho"
@klmr
klmr / memoize.r
Last active August 29, 2015 14:11
Memoize R functions
memoize = function (f) {
force(f)
cache = new.env()
function (...) {
key = capture.output(dput(list(...)))
if (key %in% ls(cache))
return(get(key, envir = cache))
value = f(...)
assign(key, value, envir = cache)
@klmr
klmr / biomart.r
Last active August 29, 2015 14:10
BiomaRt weirdness
library(biomaRt)
ensembl = useMart('ensembl', dataset = 'mmusculus_gene_ensembl')
seq = getBM(c('ensembl_gene_id', 'ensembl_transcript_id', 'coding'),
filters = 'external_gene_name', values = 'HAT1', mart = ensembl)
head(seq, 1)
@klmr
klmr / working.plist
Created October 23, 2014 16:23
Quicksilver settings of working application after manual reinstall
{
"Browse Mode" = 3;
CalSuccessfulLaunchTimestampPreferenceKey = "3.580207e+08";
"Calculator Results Display" = 1;
"Check for Update Frequency" = 1;
"Check for Updates" = 1;
"Delay Before Quitting" = 1;
"Hide Dock Icon" = 1;
"Last Update Check" = "2014-10-22 16:06:08 +0000";
"Last Used Location" = "/Applications/Quicksilver.app";
@klmr
klmr / read_table.r
Created September 29, 2014 12:51
Auto-detect file format in read.table
read.table = function (file, ..., text) {
args = list(...)
if (missing(file))
return(do.call(utils::read.table, c(args, text = text)))
if (! ('sep' %in% names(args))) {
separators = list('.csv' = ',',
'.tsv' = '\t',
'.txt' = '\t')
extension = stringr::str_match(file, '(\\.\\w+)$')[, 2]