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 August 14, 2024 17:26
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 / 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"))
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 / jq.md
Last active June 8, 2018 08:37
Some jq examples translated from https://github.com/jsonlines/guide
@jeroenjanssens
jeroenjanssens / gist:549087b0fd6551064e57
Last active January 5, 2016 14:40
RStudio Stack trace
## Run RStudio Desktop
$ rstudio-bin
## Attach debugger
$ sudo gdb -p $(pgrep rsession)
(gdb) cont
Continuing.
## In RStudio, try to connect to Aster
> library(TeradataAsterR)
@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 / 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 / case-citibikes.sh
Last active August 29, 2015 14:07
Data Science at the Command Line Strata Tutorial
# make sure that you have the R package `ggmap` installed
curl -s http://api.citybik.es/citi-bike-nyc.json > citibikes.json
< citibikes.json jq -r '.[] | [.lat/1000000,.lng/1000000,.bikes] | @csv' | header -a lat,lng,bikes > citibikes.csv
< citibikes.csv Rio -vge 'require(ggmap); qmap("NYC", zoom=14) + geom_point(data=df, aes(x=lng, y=lat, size=bikes))' > citibikes.png