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
# Data visualisation in R
```{r echo=FALSE, message=FALSE}
# Preamble.
# To set the options below.
library(knitr)
# For pretty-printed tables.
library(pander)
@klmr
klmr / test.r
Created November 12, 2015 22:53
# https://github.com/klmr/modules
library(modules)
dplyr = import_package('dplyr', attach = TRUE)
vec = import_package('S4Vectors')
# Invokes dplyr::rename:
cars %>% tbl_df() %>% rename(Slowness = speed)
# Invoke S4Vectors::rename
vec$rename(head(cars), speed = 'Slowness')
@klmr
klmr / require_fail.r
Last active September 28, 2015 10:39
require(dplyr) # fails
x = data.frame(y = 1 : 100)
y = 1
filter(x, y == 1)
# No error, but different result depending on whether `require` succeeded.
@klmr
klmr / appeal-to-authority.md
Last active August 27, 2015 14:34
Appeal to authority

Here’s what the first hits on a Google search for “appeal to authority” say (top list, no hit was omitted).

This fallacy is committed when the person in question is not a legitimate authority on the subject. […] The following standards are widely accepted:

  1. The person has sufficient expertise in the subject matter in question.
  2. The claim being made by the person is within her area(s) of expertise.
  3. There is an adequate degree of agreement among the other experts in the subject in question.
@klmr
klmr / for.r
Last active November 5, 2015 13:53
Properly scoped `for` in R.
`for` = function (var, seq, expr) {
eval(substitute(.Primitive('for')(var, seq, expr),
list(var = substitute(var),
seq = substitute(seq),
expr = substitute(expr))))
}
for (i in 1 : 5) {
j = i
cat(i)
@klmr
klmr / log-command
Last active August 29, 2015 14:27
Command line drop-in substitute to log diagnostics; simply create a symlink to this wrapper with the target somewhere inside the $PATH, and its name that of a tool to wrap.
#!/usr/bin/env bash
logfile="${SHELL_USAGE_LOG-$HOME/shell-usage.log}"
cmd=$(basename "$0")
real_cmd=$(type -ap "$cmd" | sed -n 2p)
echo "[$(date +"%Y-%m-%dT%H:%M")] $(whoami) $SHELL $cmd" >> "$logfile"
$real_cmd "$@"
@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 / main.cpp
Created June 11, 2015 14:03
How do I reverse a binary tree?
#include <iostream>
#include <memory>
#include <stack>
#include <utility>
namespace klmr { namespace util { namespace binary_tree {
template <typename T>
using ptr = std::unique_ptr<T>;
@klmr
klmr / base.r
Created May 21, 2015 13:34
Test Shiny app using R modules
title = function (title)
titlePanel(title)
@klmr
klmr / long.cpp
Created February 19, 2015 15:37
GDI golf
// stdafx.h isn’t needed.
// However, we DO need the line break after the include,
// even in the golf version.
#include <windows.h>
// _tmain => main; VC++ accepts both.
int main()
{
HDC h = GetDC(0);
// Remove i++ here …
for (int i = 0; i < 50;){