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
@hannic
hannic / ROT13.sh
Last active January 18, 2023 04:17
ROT13/ROT47 is an example of the Caesar cipher, developed in ancient Rome. Unix command. tr transliterate
# encode
tr 'A-Za-z' 'N-ZA-Mn-za-m' <<<"The Quick Brown Fox Jumps Over The Lazy Dog"
# decode
echo "Gur Dhvpx Oebja Sbk Whzcf Bire Gur Ynml Qbt" | tr '[A-Za-z]' '[N-ZA-Mn-za-m]'
#include "mach_gettime.h"
#include <mach/mach_time.h>
#define MT_NANO (+1.0E-9)
#define MT_GIGA UINT64_C(1000000000)
// TODO create a list of timers,
static double mt_timebase = 0.0;
static uint64_t mt_timestart = 0;
@klmr
klmr / fib.py
Last active September 14, 2022 08:39
Efficient, tail recursive fibonacci implementation for R and Python (but since they doesn’t do TCO it’s still using O(n) space)
def fib(n: int) -> int:
def f(n, a, b):
if n == 0: return a
if n == 1: return b
return f(n - 1, b, a + b)
return f(n, 0, 1)
@klmr
klmr / generator.md
Last active August 28, 2022 02:26
Python-like generators in R

A little experiment using restarts.

(And while we’re at it, let’s torture R’s syntax a little.)

![screenshot][]

In the following we will be using R’s “restarts” feature to implement the state machine that drives generators in languages such as Python. Generators allow lazily generating values on demand: a consumer invokes a generator, and consumes values as they are produced. A new value is only produced once the previous one has been consumed.

@klmr
klmr / gun-deaths.rmd
Last active May 5, 2022 11:19
Gun ownership vs gun deaths reanalysis
---
author: "<a href=\"http://twitter.com/klmr\">@klmr</a>"
title: "Gun deaths and gun ownership in the USA"
date: 2015-06-19
output:
html_document:
theme: readable
---
```{r echo=FALSE}
library(dplyr, warn.conflicts = FALSE, quietly = TRUE)
@klmr
klmr / rs.r
Last active February 17, 2022 08:50
A versatile re-source file function for R
#' (Re-)source parts of a file
#'
#' \code{rs} loads, parses and executes parts of a file as if entered into the R
#' console directly (but without implicit echoing).
#'
#' @param filename character string of the filename to read from. If missing,
#' use the last-read filename.
#' @param from first line to parse.
#' @param to last line to parse.
#' @return the value of the last evaluated expression in the source file.
@mikelove
mikelove / accession2url.R
Created June 16, 2016 18:33
ENA accession to URL
accession2url <- function(x) {
prefix <- "ftp://ftp.sra.ebi.ac.uk/vol1/fastq"
dir1 <- paste0("/",substr(x,1,6))
dir2 <- ifelse(nchar(x) == 9, "",
ifelse(nchar(x) == 10, paste0("/00",substr(x,10,10)),
ifelse(nchar(x) == 11, paste0("/0",substr(x,10,11)),
paste0("/",substr(x,10,12)))))
paste0(prefix,dir1,dir2,"/",x)
}
@klmr
klmr / s4-vs-s3.md
Last active June 16, 2019 17:59
A simple side-by-side comparison of S4 with S3 in R

S4

setClass('Seq', representation(data = 'character', id = 'character'),
         validity = function (object) is_dna(object@data))

print_fasta = function (object) {
@hrbrmstr
hrbrmstr / gist_comments_feed.R
Last active February 24, 2018 13:52
GitHub Gist Comments Feed Generator in R (this is how much I hate Ruby)
# Roll your own GitHub Gist Comments Feed in R
library(xml2) # github version
library(rvest) # github version
library(stringr) # for str_trim & str_replace
library(dplyr) # for data_frame & bind_rows
library(pbapply) # free progress bars for everyone!
library(XML) # to build the RSS feed
who <- "hrbrmstr" # CHANGE ME!
@klmr
klmr / mutate_when.r
Last active November 21, 2017 19:47
Missing `mutate_when` function for dplyr
modules::import_package('rlang', attach = TRUE)
mutate_when = function (.data, .filter, ...) {
dots = dots_definitions(...)$dots
rows = eval_tidy(enquo(.filter), .data)
.data[rows, names(dots)] =
lapply(dots, eval_tidy, data = .data[rows, , drop = FALSE])
.data
}