Skip to content

Instantly share code, notes, and snippets.

View jeroenjanssens's full-sized avatar

Jeroen Janssens jeroenjanssens

View GitHub Profile
@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
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)
))
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),
@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 / itermkeymap.py
Last active July 13, 2025 20:47
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-=[];'\,./`"