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 / 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.
@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)
box::use(
dplyr[...],
glue[glue],
grid,
ld = lubridate,
ragg[...],
rvest,
scales[label_date_short, label_percent],
tidyr[...],
./screen_size[...],
@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 / list_comprehension.r
Created February 12, 2016 15:17
Haskell-like list comprehension for R
# Dummy object, only required for name resolution.
set = structure(list(), class = 'set')
print.set = function (x, ...) invisible(x)
`[.set` = function (set, expr, filter) {
expr = substitute(expr)
filter = substitute(filter)
stopifnot(identical(expr[[1]], quote(`<-`)))
stopifnot(identical(expr[[2]][[1]], quote(`|`)))
@klmr
klmr / read_file.cpp
Last active April 7, 2021 20:29
“Canonical” code to slurp a file in C++17
auto read_file(std::string_view path) -> std::string {
constexpr auto read_size = std::size_t{4096};
auto stream = std::ifstream{path.data()};
stream.exceptions(std::ios_base::badbit);
auto out = std::string{};
auto buf = std::string(read_size, '\0');
while (stream.read(& buf[0], read_size)) {
out.append(buf, 0, stream.gcount());
}
@klmr
klmr / preg_helper.php
Created December 7, 2008 21:36
Function to merge several regular expressions into one single expression.
<?php
/**
* Copyright 2008-2009 Konrad Rudolph
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
We couldn’t find that file to show.
@klmr
klmr / solution.r
Created December 17, 2020 16:11
Advent of Code day 15 in R
test_cases_desc = R'(
Given the starting numbers 1,3,2, the 2020th number spoken is 1.
Given the starting numbers 2,1,3, the 2020th number spoken is 10.
Given the starting numbers 1,2,3, the 2020th number spoken is 27.
Given the starting numbers 2,3,1, the 2020th number spoken is 78.
Given the starting numbers 3,2,1, the 2020th number spoken is 438.
Given the starting numbers 3,1,2, the 2020th number spoken is 1836.
)'
`%>%` = magrittr::`%>%`