Skip to content

Instantly share code, notes, and snippets.

View burchill's full-sized avatar
💭
I love my default GitHub picture

Zach Burchill burchill

💭
I love my default GitHub picture
View GitHub Profile
@burchill
burchill / geom_segment_plus.R
Last active November 30, 2021 18:40
Updated geom_segment_plus
# an updated version of https://pastebin.com/0BRwUzpu by SO user mo-seph (https://stackoverflow.com/a/14692588/4560765)
# The version above no longer works
#' # The example:
#' points <- data.frame( x=runif(10), y=runif(10),class=1:10, size=runif(10,min=1000,max=100000) )
#' trans <- data.frame( from=rep(1:10,times=10), to=rep(1:10,each=10), amount=runif(100)^3 )
#' trans <- merge( trans, points, by.x="from", by.y="class" )
#' trans <- merge( trans, points, by.x="to", by.y="class", suffixes=c(".to",".from") )
#' ggplot( points, aes( x=x, y=y ) ) + geom_point(aes(size=size),color="red",shape=1) +
#' scale_size_continuous(range=c(4,20)) +
@burchill
burchill / set_scales_constant.R
Last active June 17, 2024 01:51
Set continuous scales to be identical across ggplot2 plots automatically
# Check https://www.zachburchill.ml/constant_scales/ 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
burchill / python_style_unpacking.R
Last active July 5, 2020 22:51
Python 3-style unpacking for variable assignment.
# 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
burchill / knit_hook_setter.R
Last active January 27, 2022 19:10
Make htmlwidgets work with knitr-powered Jekyll and GitHub Pages
#' 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
burchill / plotly_plot_maker.R
Last active June 7, 2020 04:46
Enable Plotly plots in knitr-powered Jekyll
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: https://www.zachburchill.ml/plotly_with_jekyll/ for explanation
libdirname = "js_files/",
hrefFilter = function(x) paste0("/", x)) {
deps <- list()
libdir <- paste0(postdir, libdirname)
render_deps <- function(l) {
@burchill
burchill / zachmagic.py
Last active September 10, 2024 15:09
Custom magic commands for Jupyter notebooks
# Created by Zachary Burchill, 2019-2021
# 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, %hook, %%hook, %time_beep, %%time_beep, %% %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
burchill / facet_setter.R
Last active December 4, 2022 18:00
Set coordinates for individual ggplot2 facets
if (packageVersion("ggplot2") < "3.1.0") {
stop("Need to use new version of ggplot!")
} else {
library(ggplot2)
}
UniquePanelCoords <- ggplot2::ggproto(
"UniquePanelCoords", ggplot2::CoordCartesian,
library(dplyr)
# 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
burchill / stop_floating_figures.md
Last active July 31, 2024 00:39
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
burchill / case_when.R
Last active May 18, 2018 16:31
Typed NAs and dplyr's `case_when()`
# 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
)