Skip to content

Instantly share code, notes, and snippets.

I love my default GitHub picture

Zach Burchill burchill

I love my default GitHub picture
View GitHub Profile
burchill / set_scales_constant.R
Last active Sep 23, 2020
Set continuous scales to be identical across ggplot2 plots automatically
View set_scales_constant.R
# Check for a walkthrough.
# Note that this code is slightly "better" than the code in the post, since it checks
# to see if the scales being supplied and used are all continuous.
# Examples included after the function definitions.
# Given a bunch of plots and a scale you want to apply to them, this returns that scale,
# but with limits that encompass the union of all the ranges of values in those plots
# Meant to be for users
get_shared_scale <- function(..., scale) {
burchill / python_style_unpacking.R
Last active Jul 5, 2020
Python 3-style unpacking for variable assignment.
View python_style_unpacking.R
# Imitating Python 3 unpacking in R. (requires `rlang`)
# The following code lets you unpack variables for assignment in R like you do in Python
# Examples: python ~> R
# - `a, *b, c = [1,2,3,4]` ~> `a %,*% b %,% c <- c(1,2,3,4)`
# - variables now: a = 1, b = c(2,3), c = 4
# - `a, *b, c = 1, "C"` ~> `a %,*% b %,% c <- 1 %,% "C"`
# - variables now: a = 1, b = NULL, c = "C"
# If you are using the `%,%` operator on the righthand side of the assignment, remember that the order of
# order of operations in R is weird with infix operators. You should add parentheses to any values that are calls.
burchill / knit_hook_setter.R
Last active Jun 8, 2020
Make htmlwidgets work with knitr-powered Jekyll and GitHub Pages
View knit_hook_setter.R
#' Set knitr hooks for htmlwidgets with Jekyll/GitHub Pages
#' This functions sets a `knitr` hook so that any HTML widgets that were
#' printed (i.e., objects that inherit the `'htmlwidget'`, like those from
#' the `htmlwidgets` or `plotly` packages) will work with a Jekyll system,
#' like the one used for GitHub Pages.
#' It essentially sets a hook so that, when the document is finished being
#' knitted, it moves all the dependencies necessary for the widgets to a
#' directory, and then adds HTML code to the document to load those files
burchill / plotly_plot_maker.R
Last active Jun 7, 2020
Enable Plotly plots in knitr-powered Jekyll
View plotly_plot_maker.R
plotly_manager <- function(
postdir = knitr::opts_chunk$get("plotly.savepath"), # I have this as a default knitr option. Either change or specify your own when you call it
basedir = knitr::opts_chunk$get("proj.basedir"), # Ditto. See: for explanation
libdirname = "js_files/",
hrefFilter = function(x) paste0("/", x)) {
deps <- list()
libdir <- paste0(postdir, libdirname)
render_deps <- function(l) {
burchill /
Last active Oct 13, 2020
Custom magic commands for Jupyter notebooks
# Created by Zachary Burchill, 2019
# Feel free to use/modify however you want, but be nice and
# please give me credit/attribution.
# Put this file in your jupyter directory and load it in the first cell with:
# %load_ext zachmagic
# After that, you can use %beep, %%beep, %time_beep, %%time_beep, %%noprint, %hide_all,
# %show_all, %keep_input and %%keep_input in the cells.
from IPython.core.magic import Magics, magics_class, line_magic, cell_magic, line_cell_magic
burchill / facet_setter.R
Last active Aug 26, 2020
Set coordinates for individual ggplot2 facets
View facet_setter.R
if (packageVersion("ggplot2") < "3.1.0") {
stop("Need to use new version of ggplot!")
} else {
UniquePanelCoords <- ggplot2::ggproto(
"UniquePanelCoords", ggplot2::CoordCartesian,
View keep_og_values.R
# Copied from magrittr
is_pipe <- function (pipe) {
identical(pipe, quote(`%>%`)) || identical(pipe, quote(`%T>%`)) ||
identical(pipe, quote(`%<>%`)) || identical(pipe, quote(`%$%`))
get_og_lhs <- function(expr) {
# While the expression is a call and the first element is a pipe
burchill /
Last active Nov 30, 2018
How to make figures and text stay in the order you wrote them in R Markdown when convert to LaTeX!

Keep figures and text in the order you wrote them

Want to keep the order of figures and text the way you wrote them in R Markdown when you convert to LaTeX? Tired of having to insert \FloatBarrier after every chunk you want plotted?

Just use knitr's hooks! Let's look at how we can do it:

Adding the placeins LaTeX package

First, you need to add the placeins LaTeX package to the header of your R Markdown file with \usepackage{placeins}. This lets you use the \FloatBarrier LaTeX command.

burchill / case_when.R
Last active May 18, 2018
Typed NAs and dplyr's `case_when()`
View case_when.R
# Dplyr's `case_when` function is great for doing nested `ifelse()` functions:
# it requires less keystrokes and is somewhat faster as well.
# The only "problem" is that it requires "strict typing," which can pose a problem because of NAs.
# Let's see how this works
# A data frame
df <- data.frame(
x = runif(100),
y = runif(100)+0.2 # So we have a few values > 1
burchill / hide_geoms.R
Last active Mar 7, 2018
Hide ggplot geoms without affecting legends, scaling, etc.
View hide_geoms.R
hide_geoms <- function(gg_obj) {
plot_data <- ggplot2::ggplot_build(gg_obj)
data_list = plot_data$data %>%
purrr::map(. %>% mutate(size=0,alpha=0))
plot_data$data <- data_list
# Example
df1 <- tibble(
You can’t perform that action at this time.