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_body.R
Last active December 7, 2020 23:28
Extract body from any string with \midule ... \bottomrule
extract_body <- function(gt) {
if(inherits(gt, "gt_tbl")) gt <- as.character(gt::as_latex(gt))
stringr::str_match(gt, "(?s)\\\\midrule\\n(.*)\\\\bottomrule")[[2]]
}
## GT Tables -------------------------------------------------------------------
library(tidyverse, warn.conflicts = FALSE)
library(gt, warn.conflicts = FALSE)
@kylebutts
kylebutts / ggpreview.R
Last active June 11, 2021 21:31
ggpreview from Andrew Heiss
#' Preview output from \code{ggsave()}
#'
#' Displays a preview of what a ggplot-based plot looks like after being saved
#' with \code{\link{ggsave}}. Avoids the hassle of exporting a plot, switching
#' to your system file explorer, checking the output, and returning to R to make
#' more adjustments.
#'
#' The heavy lifting here came from
#' \href{https://twitter.com/tjmahr/status/1083094031826124800?s=12}{TJ Mahr}. I
#' added the Cairo option because the Cairo graphics library can (1) properly
@kylebutts
kylebutts / export_to_png.tex
Last active March 8, 2021 17:05
Latex Table to png
\documentclass[convert={density=300,outext=.png}]{standalone}
% font
\usepackage{utopia}
\usepackage[utopia, smallerops, varg]{newtxmath}
% booktabs
\usepackage{booktabs}
% three part table
@kylebutts
kylebutts / get_min_year.R
Created March 29, 2021 14:54
From 0/1 Treatment Indicator, extract starting year
#' From an indicator for treatment, retrieve start year.
#' This function works well with
#' group_by(id) %>% mutate(group = get_min_year(year, treatment_ind))
#'
#' y = calendar time
#' t = treatment 0/1 indicator variable
get_min_year = function(y, t) {
return(y[order(y)][min(which(t[order(y)] == 1))])
}
@kylebutts
kylebutts / Tex To Png.md
Created April 25, 2021 19:32
How to convert .tex tables/figures/equations to .png

The standalone class in LaTex allows to convert individual equations, tables, and figures into .png files. It does require ImageMagick to convert pdf to image and crop

Important Note: If you are using a \begin{table}, \begin{figure}, or \begin{equation}/$$ environments, you must add the preview option to the \documentclass command or else it will not compile. For tables, I just use the \begin{tabular}\end{tabular} environment instead and it works fine and I just use $\displaystyle$ for equations.

@kylebutts
kylebutts / export_did.R
Last active May 3, 2021 20:10
Export did aggregation to tables
reg_format <- function(pt, se, crit) {
pt_str <- sprintf("%0.4f", pt)
t_stat <- abs(pt/se)
if(t_stat > crit) {
pt_str <- paste0("$", pt_str, "^{**}$")
}
return(pt_str)
@kylebutts
kylebutts / index.html
Created May 24, 2021 18:07
two-stage did post draft
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang xml:lang>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="generator" content="distill" />
@kylebutts
kylebutts / shiny_get_window.R
Last active June 12, 2021 14:41
Shiny - Window Size: gets window size and stores in input$dimension ([1] = width, [2] = height)
# From https://stackoverflow.com/questions/36995142/get-the-size-of-the-window-in-shiny
# https://stackoverflow.com/a/37060206
tags$head(tags$script('
var dimension = [0, 0];
$(document).on("shiny:connected", function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
@kylebutts
kylebutts / responsive_col.R
Created June 12, 2021 17:54
Shiny Components
responsive_col <- function(xs, ..., sm = xs, md = sm, lg = md, xl = lg, xs_offset = 0, sm_offset = 0, md_offset = 0, lg_offset = 0, xl_offset = 0){
if (!is.numeric(xs) || (xs < 1) || (xs > 12))
stop("column width must be between 1 and 12")
if (!is.numeric(sm) || (sm < 1) || (sm > 12))
stop("column width must be between 1 and 12")
if (!is.numeric(md) || (md < 1) || (md > 12))
stop("column width must be between 1 and 12")
if (!is.numeric(lg) || (lg < 1) || (lg > 12))
stop("column width must be between 1 and 12")
if (!is.numeric(xl) || (xl < 1) || (xl > 12))
makeCard <- function(..., class = "", style = "") {
div(
class = glue::glue("card ms-depth-8 {class}"),
style = style,
...
)
}
# Title within card
makeSection <- function(title) {