Skip to content

Instantly share code, notes, and snippets.

View jrnold's full-sized avatar

Jeffrey Arnold jrnold

View GitHub Profile
@jrnold
jrnold / math2png.lua
Last active April 23, 2023 04:39
Pandoc filter - math to png
--[[ pandoc filter to convert inline math to png images
Sses dvipng to convert inline and display math to png. There are better ways to handle math output,
but some platforms (e.g. Confluence) can only handle images.
Inspirations:
- https://github.com/pandoc/lua-filters/tree/master/math2svg
- The "Building images with tizk" example: https://pandoc.org/lua-filters.html#module-pandoc.template
- Sphinx imgmath from https://github.com/sphinx-doc/sphinx/blob/aee3c0ab75974790adf359a9c5089d1d781a6b21/sphinx/ext/imgmath.py#L1
@jrnold
jrnold / example.sh
Created July 19, 2022 21:45
Resolve bash script location
# original source of this is quarto
# Determine the path to this script (we'll use this to figure out relative positions of other files)
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
SCRIPT_PATH="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
export SCRIPT_PATH="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
@jrnold
jrnold / rcallbacks.py
Created June 16, 2022 18:58
Context manager to temporarily set callbacks in rpy2
import rpy2.rinterface_lib.callbacks
from typing import Callable, Optional, Tuple
class RCallbackContext:
"""Context manager for setting R callbacks.
See https://rpy2.github.io/doc/v3.5.x/html/callbacks.html#console-i-o
Attributes:
consoleread: Function to use for :py:func:`rpy2.rinterface_lib.callbacks.consoleread`
#!/usr/bin/env zsh
# Add or subtract python@
zmodload zsh/pcre
brew_python function() {
__add_version () {
version="$1"
if [[ ! "$version" =~ "^[2-3]\.[0-9]+$" ]]; then
echo "Not a valid python version" 1>&2
return 1
@jrnold
jrnold / read_sitemap.R
Created January 19, 2019 12:54
Read/write XML sitemaps in R
library("xml2")
handle_node <- function(x) {
name <- xml_name(x)
content <- xml_text(x)
if (name == "lastmod") {
content <- lubridate::ymd(content)
}
content <- list(content)
names(content) <- name
"""Run LDA models."""
import gzip
import json
import logging
import os
import os.path
import re
from argparse import ArgumentParser
from collections import OrderedDict, defaultdict
from random import shuffle, seed
@jrnold
jrnold / ancestor.R
Created June 28, 2018 01:32
Is an R environment an ancestor of another
# test if env has parent
has_ancestor <- function(env, ancestor, last = global_env(), n = NULL) {
# special cases
if (!typeof(env) == "environment") {
abort("`env` must be an environment")
}
if (is_empty_env(env)) {
return(FALSE)
}
if (!typeof(ancestor) %in% c("environment", "NULL")) {
@jrnold
jrnold / hey_gurl.R
Created June 27, 2018 18:14
Hey Gurl
# The print.warnings method only handles printing the last.warning object
# in the warnings() function.
print.warnings <- function(object,
header = "Hey Gurl \U0001f308:\n", ...) {
base:::print.warnings(object, header = header, ...)
}
# I think the message hearder is hardcoded into the do_dfltWarn() function in C
# It uses gettext so one could probably write a "translation" of that message
# and create a custom domain
@jrnold
jrnold / app.R
Created February 5, 2018 16:01
Visualizing linear regression cost function
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
@jrnold
jrnold / zipexample.R
Created January 21, 2018 04:26
Using file.exists to conditionally download a file - and multiple ways to access the files inside a zipfile in R
# Using file.exists to conditionally download a file - and multiple ways to access
# the files inside a g
library("tidyverse")
library("haven")
# name of output directory
OUTDIR <- file.path("data", "leaders")
# URL of the file
# file.path() is a "safe" way to create paths
URL <- "https://www.aeaweb.org/aej/mac/data/2008-0058_data.zip"