Skip to content

Instantly share code, notes, and snippets.

@pbchase
pbchase / purge_merged_branches.sh
Last active July 8, 2025 21:46
purge_merged_branches is a BASH function to delete branches that have been merged into the current branch
# purge_merged_branches is a BASH function to delete branches that have been merged into the current branch.
# It excludes branch names that are probably the primary branch of the source code.
# Specifically, it excludes develop, main, and master.
# Please review these choices carefully and adapt the script to your needs before using.
purge_merged_branches() {
# purge merged branches
## locally
git branch --merged | grep -v develop | grep -v main | grep -v master | xargs -i git branch -d {}
## on origin
git remote prune origin
@pbchase
pbchase / draft_next_release.sh
Last active June 25, 2025 10:57
A shell function to draft the next Changelog entry when releasing via gitflow
# Draft the next ChangeLog.md / NEWS.md entry when releasing via gitflow
# by enumerating all the commits since the last release and displaying
# them below release header. It formats everything in a markdown-friendly
# syntax
#
# **Caveats** - so many caveats
# This command requires releases to be tagged with version numbers
# that conform to "sort --version-sort".
#
# This function produces better results if you use gitflow and are on the
@pbchase
pbchase / gfrc_bash_profile_addition.sh
Last active June 15, 2025 12:51
Expedite release tasks during a `git flow release`
# gfrc - Git Flow Release Commit
# Expedite updates to a CHANGELOG.md or NEWS.md file,
# R Package DESCRIPTION file, and CITATION.cff
# during a `git flow release`
# Thanks to Kyle Chesney for some great ideas.
gfrc() {
if [ -f 'CHANGELOG.md' ]; then
CL_FILENAME=CHANGELOG.md
else
@pbchase
pbchase / sync.sh
Created February 10, 2025 19:51
REDCap module sync script
#!/bin/bash
if [ $# -eq 0 ]; then
echo "sync.sh: a REDCap module sync script"
echo "Use:"
echo "./sync.sh my_module_v0.0.0 my_host.ini"
echo "Optional flags (place after other arguments)"
echo "--version=x.y.z: change the version on the target server, defaults to contents of VERSION"
echo "USER: override the USER set in your ini file"
echo "HOST: override the HOST set in your ini file"
echo "PATH_TO_MODULES_DIR: override the PATH_TO_MODULES_DIR set in your ini file"
@pbchase
pbchase / make_grid_of_hyperlinks_in_xlsx.R
Created June 6, 2024 19:23
How to make a grid of labeled hyperlinks in an XLSX with tidyverse and openxlsx
@pbchase
pbchase / pick_a_categorical_field_response.R
Created January 30, 2024 17:46
pick_a_categorical_field_response() ...given a REDCap data dictionary as input
pick_a_categorical_field_response <- function(metadata) {
response_codes <-
metadata |>
dplyr::filter(.data$field_type %in% c("checkbox", "radio", "dropdown")) |>
dplyr::select(c("field_name", "field_type", "select_choices_or_calculations")) |>
tidyr::separate_longer_delim("select_choices_or_calculations", delim = " | ") |>
tidyr::separate_wider_delim("select_choices_or_calculations", delim = ", ", names = c("response_code", "response_label"), too_many = "merge")
single_value_responses <- response_codes |>
filter(field_type != "checkbox") |>
@pbchase
pbchase / make_rdc_instance.sh
Last active September 1, 2023 17:45
Make a redcap-docker-compose instance
#!/bin/bash
# make_rdc_instance.sh
# This is a helper script for https://github.com/123andy/redcap-docker-compose
usage() {
echo "Usage: $0 [-v <redcap_version_number_sans_periods>] [-m <modules_directory>] [-s <suffix>] [-g]" 1>&2
echo " " 1>&2
echo " -v: Please provide a 3 or 4-digit REDCap version number without periods." 1>&2
echo " Using this number, this script will copy the rdc folder to a new folder and configure the environment" 1>&2
echo " with unique port numbers and instance name." 1>&2
@pbchase
pbchase / identify_log_event_consumers.R
Created July 11, 2023 21:06
Identify the projects on a REDCap system that are heavy consumers of space in a redcap_log_event table
library(tidyverse)
library(lubridate)
library(dotenv)
library(redcapcustodian) # devtools::install_github("ctsit/redcapcustodian")
library(DBI)
library(RMariaDB)
script_name <- "identify_log_event_consumers"
connect_to_redcap_with_schema <- function(schema = NULL) {
@pbchase
pbchase / convert_keepachangelog_format_for_pkgdown.R
Created March 17, 2023 22:40
Convert keep-a-change-log format to match pkgdown standards
# Convert keep-a-change-log format to match pkgdown standards
library(tidyverse)
fs::file_copy("NEWS.md", "NEWS.md.orig")
line <- readr::read_lines("NEWS.md")
tibble(line) %>%
mutate(release = str_match(line, "^## \\[(.+)\\] - ([0-9]{4}-[0-9]{2}-[0-9]{2})")) %>%
mutate(version = release[,2],
release_date = release[,3]
@pbchase
pbchase / knitr_class.Rmd
Last active February 3, 2023 14:05
sample code for a data carpentry knitr class
---
title: "My Awesome Document"
author: "Philip Chase"
date: "`r Sys.Date()`"
output: html_document
---
```{r global_options, echo=FALSE}
knitr::opts_chunk$set(fig.path="Figs/", message=FALSE, warning=FALSE,
echo=FALSE, results="hide", fig.width=11)