Skip to content

Instantly share code, notes, and snippets.

View AdamSpannbauer's full-sized avatar
👀
¯\_(ツ)_/¯

Adam Spannbauer AdamSpannbauer

👀
¯\_(ツ)_/¯
View GitHub Profile
@AdamSpannbauer
AdamSpannbauer / plot_centroids_table.R
Last active September 11, 2024 11:50
Helper to make a conditionally formatted table of kmeans centroids with ggplot2
library(ggplot2)
library(dplyr)
library(tidyr)
plot_centroids_table <- function(kmeans_object) {
n_clusters <- nrow(kmeans_object$centers)
plot_df <- data.frame(t(kmeans_object$centers))
names(plot_df) <- paste("Cluster", 1:n_clusters)
plot_df$feature_name <- rownames(plot_df)
@AdamSpannbauer
AdamSpannbauer / transaction2df.R
Created July 17, 2024 21:05
Helper to convert a transaction object to a df
library(arules)
transaction2df <- function(transation_obj) {
if (!inherits(transation_obj, "transactions")) {
error_msg <- paste0(
"\ntransactions objects only pls\n",
"\ncheck your object with: class(",
substitute(transation_obj),
")"
)
@AdamSpannbauer
AdamSpannbauer / suggest_ljung_box_lags.R
Created March 19, 2023 15:20
Small helper function for suggesting number of lags in Ljung-Box test per Rob Hyndman advice in fpp3
# From: https://otexts.com/fpp3/diagnostics.html#portmanteau-tests-for-autocorrelation
#
# We suggest using l = 10 for non-seasonal data and l = 2 * m for seasonal data, where m is the period of
# seasonality. However, the test is not good when l is large, so
# if these values are larger than T / 5 then use l = T / 5.
# Translating into R function:
# * n_obs - number of observations in the series (referred to as T in the text)
# * n_seasonal_periods - NA if non-seasonal; 4 if quarterly; 12 if monthly; etc.
@AdamSpannbauer
AdamSpannbauer / prettify_discretize_labs.R
Created November 14, 2022 13:32
Convert arules::discretize's factor levels to a more approachable format for non-technical audience
prettify_discretize_labs <- function(x, sep = " to ") {
all_labs <- levels(x)
split_labs <- strsplit(all_labs, ",")
old_options <- options(scipen = 999)
pretty_labs <- vapply(split_labs, function(el) {
num_chrs <- gsub("\\[|\\]|\\(|\\)", "", el)
nums <- as.numeric(num_chrs)
pretty_lab <- paste(nums[1], nums[2], sep = sep)
}, character(1))
@AdamSpannbauer
AdamSpannbauer / native_dash_app.py
Last active June 27, 2022 13:18
Test using PyWebView to run a Plotly Dash app as a native web app
import webview
from dash import Dash, html
def run_native_dash_app(dash_app: Dash, window_title: str = None) -> None:
"""Run dash app as native web app
Use PyWebView to run a dash app as a native web app
* install with `pip install pywebview`
* project home page: https://pywebview.flowrl.com/
@AdamSpannbauer
AdamSpannbauer / sqldf_examples.R
Created February 15, 2022 16:04
sqldf examples
# install.packages('sqldf')
library(sqldf)
data("mtcars")
# Select all
sqldf("SELECT *
FROM mtcars")
# single column
@AdamSpannbauer
AdamSpannbauer / gganimate_heart.R
Last active February 10, 2022 14:40
animated heart using the R package gganimate
library(ggplot2)
library(gganimate)
library(data.table)
gen_heart_y = function(x, a) {
# source: https://i.imgur.com/avE8cxJ.gifv
(x^2)^(1 / 3) + 0.9 * (3.3 - x^2)^(1 / 2) * sin(a * pi * x)
}
heart_dt_list = lapply(seq(1, 15, by = 0.1), function(a) {
@AdamSpannbauer
AdamSpannbauer / r6_fractal_tree.R
Last active August 16, 2021 21:13
R6 Fractal Tree
library(R6)
library(ggplot2)
library(uuid)
options(stringsAsFactors = FALSE)
branch_base = R6Class('branch_base',
public = list(
start_x = NA_integer_,
start_y = NA_integer_,
end_x = NA_integer_,
@AdamSpannbauer
AdamSpannbauer / barnsley_fern.R
Created July 23, 2018 19:14
generate/draw/animate Barnsley fern in R
# Barnsley fern in R
# reference: https://en.wikipedia.org/wiki/Barnsley_fern
library(plotly)
# FUNCTIONS FOR GENERATING BARNSLEY FERN POINTS ################################
transform_1 = function(x, y) {
x = 0
y = 0.16 * y
return(c(x, y))
@AdamSpannbauer
AdamSpannbauer / Sketch.js
Last active January 22, 2021 10:45
A base class for a p5js React Component
/*
A base class to create p5js sketches as React components.
Requires p5js: `npm install p5`
## Usage
### Extend Sketch and create a p5js sketch
```