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
@mschubert
mschubert / BatchJobsWrapper.R
Last active August 29, 2015 13:57
Wrapper for the BatchJobs library that simplifies the interface and performs more checks
library(stringr)
library(BatchJobs)
library(plyr)
# Rationale
# This script uses BatchJobs to run functions either locally, on multiple cores, or LSF,
# depending on your BatchJobs configuration. It has a simpler interface, does more error
# checking than the library itself, and is able to queue different function calls. The
# function supplied *MUST* be self-sufficient, i.e. load libraries and scripts.
# BatchJobs on the EBI cluster is already set up when using the gentoo prefix.
@tleonardi
tleonardi / bobR.sh
Created May 22, 2014 10:21
Wrapper script to run Vim-R-plugin R sessions as interactive jobs on LSF platforms
#!/bin/bash
# Wrapper script to run Vim-R-plugin R sessions as interactive jobs on LSF platforms.
# To use it, edit ~/.vimrc and add 'let vimrplugin_r_path = <path_to_script>'
# lets you deicde
echo What version of R do you want?
echo "1) R vX.X.X"
echo "2) R vZ.Z.Z"
echo "3) R vY.Y.Y"
read -t 60 -p '>' answer
@dgrtwo
dgrtwo / parsetidy.R
Last active September 16, 2015 09:10
Some thoughts on a parsetidy function that turns expressions into tidy data frames, and further processes them (for use in reprex package)
parsetidy <- function(x, ...) UseMethod("parsetidy")
parsetidy.default <- function(x, ...) {
# if it's not a call or name (e.g. it's numeric),
# don't need it
data.frame()
}
@klmr
klmr / vim-notes.md
Created January 22, 2014 11:46
Notes for the seminar “Using Vim” for the Predoc Lunch Seminar series at EMBL-EBI.

Vim Notes

Setup

The Vim on the server is horribly outdated. Many of the things below won’t work, or will require extensive setup. In order to ease the pain, I suggest using the version installed and maintained by Micha via the [EBI-predoc config][config].

@klmr
klmr / printmakevars
Last active October 20, 2016 12:43
A helper script to print make variables
#!/usr/bin/env bash
# Inspired by <http://blog.melski.net/2010/11/30/makefile-hacks-print-the-value-of-any-variable/>
usage() {
echo >&2 "Usage: $0 [-f makefile] variables..."
echo >&2
echo >&2 "Optional arguments:"
echo >&2 " -f makefile: path to the makefile"
echo >&2
@klmr
klmr / curry.md
Last active February 14, 2017 14:57
Currying explained

In the following, let’s define sum as

sum = function (a, b, c) a + b +c

Because base::sum’s definition involves ..., which would make the following explanation unnecessarily complex.


@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 / 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 / README.md
Last active April 27, 2017 14:28
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:

debug_random_seed()
sample(10)
@randy3k
randy3k / importCpp.R
Last active June 4, 2017 23:12
a wrapper of sourceCpp which keeps the shared library file.
importCpp <- function(infile, output_dir="lib", rebuild=FALSE){
output_dir = ifelse(is.null(output_dir), ".", output_dir)
dir.create(output_dir, recursive=T, showWarnings=FALSE)
outfile = file.path(output_dir, paste0(infile, ".R"))
if (!file.exists(outfile) || file.info(infile)$mtime > file.info(outfile)$mtime || rebuild){
Rcpp::sourceCpp(infile, rebuild=rebuild)
context = .Call("sourceCppContext", PACKAGE = "Rcpp",
normalizePath(infile, winslash = "/"), code=NULL, 0L, .Platform)
scriptfile = file.path(context$buildDirectory, context$rSourceFilename)