Skip to content

Instantly share code, notes, and snippets.

View jrnold's full-sized avatar

Jeffrey Arnold jrnold

View GitHub Profile
@jrnold
jrnold / supabase-aliaes.sh
Created May 11, 2024 18:06
Supabase aliases for local dev
alias supabase-dashboard="open $(supabase status -o json | jq -r '.STUDIO_URL')"
alias supabase-psql="psql $(supabase status -o json | jq -r '.DB_URL')"
"""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 / 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 / gist:6799152
Last active August 18, 2021 13:44
Create a plot of the normal distribution with an area shaded in. Useful for teaching z-scores and stuff like that.
library("ggplot2")
#' Draw Normal Distribution Density with an area shaded in.
#'
#' @param lb Lower bound of the shaded area. Use \code{-Inf} for a left tail.
#' @param ub Upper bound of the shaded area. Use \code{Inf} for a right tail.
#' @param mean Mean of the normal distribution
#' @param sd Standard deviation of the normal distribution
#' @param limits Lower and upper bounds on the x-axis of the area displayed.
#' @return ggplot object.
@jrnold
jrnold / axis_text_size.R
Last active June 7, 2021 15:19
Changing axis title and text size in ggplot with theme_economist
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg,
colour=factor(gear))) +
facet_wrap(~am)
# Change the size of axis titles to be 2 x that of the default text size using function rel
# Change the size of the axis text to be 8 pt
p + theme_economist() + scale_colour_economist() +
theme(axis.title = element_text(size = rel(2)),
axis.text = element_text(size = 8)
@jrnold
jrnold / globaltemp.R
Created February 3, 2013 03:06
Kalman filter in Stan
library(KFAS)
library(rstan)
data(GlobalTemp)
model_dlm1a <- stan_model("../stan/dlm1a.stan")
y <- as.matrix(GlobalTemp)
data <-
within(list(),
{
@jrnold
jrnold / server.R
Last active May 6, 2020 18:39
Shiny app comparing the t-distribution to the normal distribution
library("shiny")
x <- seq(-4, 4, by=0.01)
norm_dens <- dnorm(x)
shinyServer(function(input, output) {
output$plot <- renderPlot({
t_dens <- dt(x, df = input$df)
print(ggplot()