Skip to content

Instantly share code, notes, and snippets.

View jrnold's full-sized avatar

Jeffrey Arnold jrnold

View GitHub Profile
jrnold / math2png.lua
Last active April 23, 2023 04:39
Pandoc filter - math to png
View math2png.lua
--[[ 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.
- The "Building images with tizk" example:
- Sphinx imgmath from
jrnold /
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)
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
export SCRIPT_PATH="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
jrnold /
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.
consoleread: Function to use for :py:func:`rpy2.rinterface_lib.callbacks.consoleread`
View brew_python.zsh
#!/usr/bin/env zsh
# Add or subtract python@
zmodload zsh/pcre
brew_python function() {
__add_version () {
if [[ ! "$version" =~ "^[2-3]\.[0-9]+$" ]]; then
echo "Not a valid python version" 1>&2
return 1
jrnold / read_sitemap.R
Created January 19, 2019 12:54
Read/write XML sitemaps in R
View read_sitemap.R
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
View gist:daa039f02486009a24cf3e83403dabf0
"""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 / ancestor.R
Created June 28, 2018 01:32
Is an R environment an ancestor of another
View ancestor.R
# 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)) {
if (!typeof(ancestor) %in% c("environment", "NULL")) {
jrnold / hey_gurl.R
Created June 27, 2018 18:14
Hey Gurl
View hey_gurl.R
# 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 / app.R
Created February 5, 2018 16:01
Visualizing linear regression cost function
View app.R
# 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:
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
View zipexample.R
# Using file.exists to conditionally download a file - and multiple ways to access
# the files inside a g
# name of output directory
OUTDIR <- file.path("data", "leaders")
# URL of the file
# file.path() is a "safe" way to create paths
URL <- ""