Skip to content

Instantly share code, notes, and snippets.

View jimhester's full-sized avatar

Jim Hester jimhester

View GitHub Profile
@jimhester
jimhester / remotes.R
Created October 3, 2018 22:51
remotes
# Clone the happy-git-with-r repository locally
# List the current remotes
# Hint: use `git remote -v` for more information
# Fork the repository on GitHub
# Add your forked remote (call it 'mine')
# Hint: `git remote add`
@jimhester
jimhester / branch.R
Last active October 3, 2018 22:46
git branching activities
# Start a new git project
# Hint: (`git init`) or in RStudio `File -> New Project -> check Create a git repository`
# All students, Pick up your wands
# Hint: Open a text file add 'pick up wand' to it, and commit the result
# Have Ron (try) to cast wingardium leviosa
# Remember: Ron pronounces this 'Wingardium Leviosaaaaaaaa'
# Hint: create the Ron branch and check it out, then commit Ron's spell on a new line
@jimhester
jimhester / gist:fb30685d6c5c370d1a5bf18429c97821
Created September 14, 2018 17:34
packages with assignment methods
# find . -name 'NAMESPACE' -type f | xargs -P 8 rg 'export\(.*<-' -i -l | perl -pe 's!/NAMESPACE!!'
abind
adegenet
ade4
AMModels
analyz
apex
arrayhelpers
arsenal
assertive.base
@jimhester
jimhester / create_flashcards.R
Last active May 20, 2018 13:23
Create flash cards of rOpenSci unconf participants. You can get the shared deck from https://ankiweb.net/shared/info/806702790 or by importing the 'unconf 2018.apkg' file in this gist
# On macOS Anki media files are stored in ~/Library/Application Support/Anki2/Username/collection.media/
# https://apps.ankiweb.net/docs/manual.html#file-locations
# get images from Unconf repo, resize them and write them to the new path
# Code assumes it is being run on macOS from the checkout of the conference repo, e.g. `git clone https://github.com/ropensci/unconf18 && cd unconf18`
library(fs)
library(magick)
imgs <- dir_ls("images/participants")
for (f in imgs) {
f %>%
image_read() %>%
@jimhester
jimhester / profile_gperftools.md
Last active April 12, 2018 13:48
Instructions for using gperftools on macOS with R packages

Initial setup

Install gpreftools

brew install gperftools

Add linking flags to ~/.R/Makevars

LDFLAGS=`pkg-config libprofiler --libs`
#include <stdint.h>
#include <stdlib.h>
#include <Rcpp.h>
// From https://en.m.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
#define ALPHABET_LEN 256
#define NOT_FOUND patlen
#define max(a, b) ((a < b) ? b : a)
// delta1 table: delta1[c] contains the distance between the last
@jimhester
jimhester / bench.R
Last active March 29, 2018 14:47
getOption benchmark
getOptionOld <- function(x, default = NULL) {
## To avoid always performing the %in%,
## we use the original code if default is not specified.
## if(missing(default)) return(options(x)[[1L]])
if(missing(default) || x %in% names(.Options))
.Internal(getOption(x))
else
default
}
@jimhester
jimhester / graphql_reviews.R
Last active April 27, 2018 15:31
graphql query
library(tidyverse)
library(httr)
library(jsonlite)
library(glue)
library(lubridate)
# JSON friendly version of glue
jlue <- function(..., .envir = parent.frame()) {
glue(..., .envir = .envir, .open = "<", .close = ">")
}
# foo() just calls sample
foo <- function() invisible(sample(1))
# foo2() sets a new seed
foo2 <- function() set.seed(42)
@jimhester
jimhester / enum.R
Created December 4, 2017 18:19
Simple C style enum in R.
enum <- function(...) {
nms <- eval(substitute(alist(...)))
x <- as.list(setNames(seq_along(nms), nms))
f <- tempfile()
attach(x, name = f)
# Cleanup the enum if when the parent frame exists
# unless we are in the global environment.
if (!identical(parent.frame(), globalenv())) {
withr::defer(detach(name = f), parent.frame())
}