Skip to content

Instantly share code, notes, and snippets.

View jeroenjanssens's full-sized avatar

Jeroen Janssens jeroenjanssens

View GitHub Profile
@jeroenjanssens
jeroenjanssens / itermkeymap.py
Last active January 27, 2023 20:55
Generate iTerm Key Mappings with Python
#!/usr/bin/env python3
import json
import string
prefix_key = "b" # My prefix key in tmux is Ctrl-B
prefix_hex = "02" # The hex code that iTerm sends (corresponds to Ctrl-B)
keys_upper = string.ascii_uppercase + r'!@#$%^&*()_+{}:"|<>?~'
keys_lower = string.ascii_lowercase + r"1234567890-=[];'\,./`"
@jeroenjanssens
jeroenjanssens / chat.sh
Last active February 15, 2022 21:44
Simple chat server in bash, demonstrating websocketd.
#!/bin/bash
# Hacked together by JeroenJanssens.com on 2013-12-10
# Requires: https://github.com/joewalnes/websocketd
# Run: websocketd --devconsole --port 8080 ./chat.sh
echo "Please enter your name:"; read USER
echo "[$(date)] ${USER} joined the chat" >> chat.log
echo "[$(date)] Welcome to the chat ${USER}!"
tail -n 0 -f chat.log --pid=$$ | grep --line-buffered -v "] ${USER}>" &
while read MSG; do echo "[$(date)] ${USER}> ${MSG}" >> chat.log; done
@jeroenjanssens
jeroenjanssens / cache.R
Last active September 25, 2020 14:33
Cache the result of an expression in R
#' Cache the result of an expression.
#'
#' Use \code{options(cache.path = "...")} to change the cache directory (which
#' is the current working directory by default).
#'
#' @param expr expression to evaluate
#' @param key basename for cache file
#' @param ignore_cache evalute expression regardless of cache file?
#' @return result of expression or read from cache file
#'
@jeroenjanssens
jeroenjanssens / pre-commit
Created December 18, 2018 12:40
Script that tests your #rstats code before committing to git. When you have failed tests the commit will be aborted. Requires the devtools, testthat, and purrr packages. Name this script `pre-commit`, make it executable, and place it in the .git/hooks subdirectory of your git repository.
#!/usr/bin/env RScript
messages <- purrr::map_chr(devtools::test(),
list("results", 1, "message"))
q("no", status = sum(messages != "success"))
@jeroenjanssens
jeroenjanssens / config.md
Last active June 8, 2018 08:39
Sensitive information in R scripts

If your R script uses senstive information such as a password, then it's best to keep this in a seperate file (and perhaps outside the project's repository). Moreover, if you're giving a live demo using RStudio, then you should avoid putting this senstive information in your global environment.

If you put it in a YAML file, say .my_project.yaml, which may look as follows:

---
api_service:
  username: foo
  password: bar123!
@jeroenjanssens
jeroenjanssens / jq.md
Last active June 8, 2018 08:37
Some jq examples translated from https://github.com/jsonlines/guide
library(tidyverse)
library(magrittr)
library(stringr)
library(readxl)
directory <- "bunch/of/excel/files"
# Get an overview of all the Excel files and their sheets
sheets <-
data_frame(file = list.files(directory, full.names = TRUE),
library(rvest)
html_more_nodes <- function(session, css, more_css) {
xml2:::xml_nodeset(c(
html_nodes(session, css),
tryCatch({
html_more_nodes(follow_link(session, css = more_css),
css, more_css)
}, error = function(e) NULL)
))
@jeroenjanssens
jeroenjanssens / keep_top_n.R
Created April 12, 2017 10:12
R function to keep rows belonging to top n groups of a certain column
library(tidyverse)
keep_top_n <- function(df, col, n = 10) {
semi_join(df, head(count_(df, col, sort = TRUE), n))
}
data(mpg)
# All car models
mpg %>% nrow()
# Just car models of top three manufacturers
@jeroenjanssens
jeroenjanssens / README.md
Last active March 1, 2016 14:08
Stem-and-Leaf Plot

Back in the old days, when many data sets were still small, stem-and-leaf plots were a popular method of representing quantitative data. The example data shown in the text area comes from the cover of John Tukey's Exploratory Data Analysis. The stem-and-leaf plot updates as you change the data. Try adding fractions and negative values. Hover over the leaves to see the original values.