Skip to content

Instantly share code, notes, and snippets.

View debruine's full-sized avatar
🏳️‍🌈

Lisa DeBruine debruine

🏳️‍🌈
View GitHub Profile
@debruine
debruine / prog_pride.R
Last active June 1, 2022 13:35
Create a progressive pride flag with code
# colors via https://twitter.com/dataandme/status/1531946768270860288
pride <- c(
red = '#E50000',
orange = '#FF8D00',
yellow = '#FFEE00',
green = '#028121',
blue = '#004CFF',
purple = '#760088',
black = '#000000',
brown = '#613915',
@debruine
debruine / sortviz.R
Created May 9, 2022 09:33
Visualise a sorting algorithm
library(ggplot2)
library(dplyr)
# make starting vector
A = LETTERS[5:1]
# pre-allocate matrix to store steps
n = length(A)
steps <- matrix(nrow = (n*n)+1,
ncol = 2 + n)
@debruine
debruine / pkg_finder.R
Last active April 19, 2022 18:40
Find all packages in an R project loaded with library(pkg) or pkg::func()
# find all packages used in this project ----
# include this file to test odd library formations:
library( 'dplyr' ) # for pipes
library("purrr") # for iteration
library( stringr ) # for text extraction
# find files that might have library() or pkg::func() notation
# EDIT FOR YOUR PROJECT
qmd_files <- list.files(pattern = "\\.qmd$")
@debruine
debruine / tables_demo.qmd
Last active May 26, 2022 15:05
Testing methods for df_print-like custom table printing in quarto
---
title: "Table printing demo"
author: "Lisa DeBruine"
toc: true
toc_float: true
---
## Code
These functions should override `knitr::knit_print()` for data frames, but wasn't working at all until I learned in [the knit_print vignette](https://cran.r-project.org/web/packages/knitr/vignettes/knit_print.html) that you have to use `registerS3method()`.
@debruine
debruine / stained_glass_heart.R
Created April 16, 2022 12:34
Make a stained glass heart with ggplot
# inpired by https://github.com/IcaroBernardes/30DayChartChallenge/blob/master/2022/day10/day10.R
library(ggplot2)
library(dplyr)
library(sf)
library(ggforce) # for voroni tiles
seed <- 8675309
n_pts <- 150 ### Number of points to try to put inside the window
palette <- c(
@debruine
debruine / figcap_replace.R
Last active April 15, 2022 12:19
Replace figure captions in r chunk headers in Rmd files with text captions
# replace figure captions like:
#
# ```{r myfigure, fig.cap = "This is my figure."}
#
# with text captions like this:
#
# (ref:myfigurelab) This is my figure.
#
# ```{r myfigure, fig.cap = "(ref:myfigurelab)"}
@debruine
debruine / margintable.R
Last active March 15, 2022 14:51
Make crosstabs and add margin totals (or means); a tidyverse-friendly version of base::table()
#' Crosstabs with margins
#'
#' @param data Data frame or tibble
#' @param row Column name (string or index) for rows
#' @param col Column name (string or index) for columns
#' @param margin_func Margin function (e.g., sum, mean, median)
#' @param margin_label Label for margin column and row
#' @param col_prefix Prefix for columns (defaults to col name), set to FALSE to omit; If return == "kable", this is used for the grouping header
#' @param return Return a tibble or formatted kableExtra table
#' @param ... Arguments to pass to kableExtra::kable()
@debruine
debruine / findr.R
Last active February 22, 2022 16:04
Find r given inferential stat and descriptives for a within-subject design
library(faux)
library(afex)
f <- function(r, reported) {
# simulate data for 3-level within design (change for your design)
dat <- faux::sim_design(
# you can add these parameters to f() arguments
# if you need to check varying analyses
n = 100, within = 3,
mu = c(100, 105, 110),
@debruine
debruine / doodle.R
Last active February 12, 2022 14:10
Deal with the terrible doodle format
library(dplyr)
library(tidyr)
library(readxl)
library(lubridate)
# deal with multi-line headers
data_head <- readxl::read_excel("Doodle.xls",
skip = 3, n_max = 3,
col_names = FALSE)
@debruine
debruine / ranfac.R
Last active February 1, 2022 18:24
Simulation for random factors
library(faux)
library(tidyverse)
library(lme4)
# simulate nested data with 12 subjects tested at 6 time points with 100 observations per time point
dat <- add_random(ID = 12) %>%
add_random(Time = 1:6) %>%
add_within(rep = 1:100) %>%
add_ranef(.by = "ID", id_int = 2, id_slope = 1) %>%
add_ranef(.by = "Time", time_int = 1) %>%