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 / package_list.md
Last active February 15, 2019 19:44
A list of some packages / data analysis repos I've made

Packages / data analysis repos I've made

Because I mostly work with human subject data, most of my projects are not able to be made public due to the IRB protocols. However, I've managed to excise some parts of the projects that do not have personally identifiable/demographic information, as well as excerpts from computational projects and other R-related activities I've done.

  • catchr 0.2.0: This the exception to the rule about not being complete--this is my first CRAN-accepted package, and is the only package I've really "done right". Designed to provide a shallower learning curve for condition-handling in R, catchr offers a nice little set of tools for working with conditions in R, utilizing a very simple domain-specific language. catchr 0.1.0 was a very different package, but after a deap dive into the rlang source code, I realized most of it was redundant and redid everything from the ground up. I'm actually relatively proud of this one. For a (so
@burchill
burchill / hide_geoms.R
Last active November 3, 2021 19:19
Hide ggplot geoms without affecting legends, scaling, etc.
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
return(ggplot2::ggplot_gtable(plot_data))
}
# Example
df1 <- tibble(
@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
)
@burchill
burchill / stop_floating_figures.md
Last active May 24, 2022 07:57
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.

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 / 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,
@burchill
burchill / zachmagic.py
Last active December 16, 2021 18:48
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 / 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 / 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 / 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.