Skip to content

Instantly share code, notes, and snippets.

View kylebutts's full-sized avatar

Kyle F Butts kylebutts

View GitHub Profile
@kylebutts
kylebutts / extract_within.R
Last active June 21, 2021 16:20
Common spatial GIS tasks in `sf`
#' For each point in x, identify which geometry in y the point falls within and extract value of variable `var`
#'
#' @param x `sf` object consisting of points
#' @param y `sf` object consisting of geometries
#' @param var character of variable name you wish to extract from `y`
#'
extract_within <- function(x, y, var) {
if(!inherits(x, "sf") | !inherits(y, "sf")) {
stop("Both x and y must be sf objects")
}
@kylebutts
kylebutts / did2s_falsification_test.R
Created June 23, 2021 13:07
did2s Falsification Test
library(tidyverse)
library(haven)
#' From an indicator for treatment, retrieve start year.
get_min_year = function(y, t) {
return(y[order(y)][min(which(t[order(y)] == 1))])
}
data <- haven::read_dta("nlswork.dta")
@kylebutts
kylebutts / codechella.R
Last active July 15, 2021 16:58
Event Study Estimators for Codechella
# Load libraries
library(tidyverse)
library(haven)
library(fixest)
library(did2s)
library(did)
# Install package
# install.packages("tidyverse")
# install.packages("haven")
@kylebutts
kylebutts / fact_to_sparse.R
Created August 2, 2021 16:58
Factors to sparse matrix efficiently
#' Creates sparse 0/1 matrix for factor variables
#'
#' @param df dataframe containing factor columns that will be turned into sparse matrix.
#' Note that the variables aren't required to be factor variables themselves.
#' Make sure not to include other variables in df
fact_to_sparse <- function(df) {
# Convert to factor variables
df[,names(df)] <- lapply(df[,names(df)] , factor)
@kylebutts
kylebutts / email_script.R
Created October 6, 2021 02:11
How to Blastula
library(tidyverse)
library(here)
library(blastula)
# Setup ----
# create_smtp_creds_key(
# id = "kybu6659",
# user = "kybu6659@colorado.edu",
# provider = "gmail"
# )
@kylebutts
kylebutts / data.js
Last active November 15, 2021 22:49
Discrete R Color Palettes
const palette_index = [{"package":"awtools","palette":"a_palette","length":8,"type":"sequential","novelty":true,"key":"awtools::a_palette"},{"package":"awtools","palette":"ppalette","length":8,"type":"qualitative","novelty":true,"key":"awtools::ppalette"},{"package":"awtools","palette":"bpalette","length":16,"type":"qualitative","novelty":true,"key":"awtools::bpalette"},{"package":"awtools","palette":"gpalette","length":4,"type":"sequential","novelty":true,"key":"awtools::gpalette"},{"package":"awtools","palette":"mpalette","length":9,"type":"qualitative","novelty":true,"key":"awtools::mpalette"},{"package":"awtools","palette":"spalette","length":6,"type":"qualitative","novelty":true,"key":"awtools::spalette"},{"package":"basetheme","palette":"brutal","length":10,"type":"qualitative","novelty":true,"key":"basetheme::brutal"},{"package":"basetheme","palette":"clean","length":10,"type":"qualitative","novelty":true,"key":"basetheme::clean"},{"package":"basetheme","palette":"dark","length":10,"type":"qualitative
@kylebutts
kylebutts / index.html
Created November 16, 2021 21:24
For Mason
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mason Butts | Design</title>
<script src="https://cdn-tailwindcss.vercel.app/?plugins=forms,typography,aspect-ratio,line-clamp"></script>
@kylebutts
kylebutts / html2R.R
Created November 18, 2021 00:33
Convert raw html to shiny/htmltools tags
#' Convert raw html to htmltools/shiny tags
#'
#' @description Note that this copies text to your clipboard
#'
#' @param raw_html Character vector of html text
#'
#' @return Character string of htmltools:: version of html
#'
#' @examples
#' raw1 <- div(
@kylebutts
kylebutts / index.html
Created November 20, 2021 00:23
Stata Regressions to R
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fixest Stata to R</title>
<!-- prism.js -->
@kylebutts
kylebutts / extract_table.py
Last active November 24, 2021 15:21
Extract table from pdf pages using Layout.Parser
# Extract images from PDFs
import pdfplumber
# Layout Parser
import layoutparser as lp
import cv2
# regex
import re