Skip to content

Instantly share code, notes, and snippets.

@friendly
friendly / update-pkgs.R
Last active August 23, 2026 21:37
Non-interactive R package updater for Windows: retries self-locked deps (cli/curl/digest/etc.) in a fresh --vanilla child process
# update-pkgs.R
#
# Update all installed R packages (CRAN + Bioconductor, if BiocManager is
# installed) in one non-interactive run. Designed to be launched with
# Rscript, e.g. from a background process, instead of RStudio's
# Tools > Check for Package Updates (which is slow and blocks the GUI).
#
# Usage (terminal command line):
# Rscript.exe path/to/update-pkgs.R
#
@friendly
friendly / pull-commit-history.R
Created August 10, 2026 19:24
Pull your GitHub commit history across multiple repos via the GraphQL API (gh CLI + R)
# Pull your GitHub commit history via the GraphQL API
#
# What was I working on, and when? How does work on one project relate to
# others? This pulls commit history for a hand-picked list of repos into one
# tidy dataset, so you can answer that with data instead of memory.
#
# ---------------------------------------------------------------------------
# What it does
# ---------------------------------------------------------------------------
# Pulls commits and their attributes from GitHub directly via `gh api
@friendly
friendly / chunks.R
Created July 24, 2026 19:25
chunks.R — Parse Quarto/knitr code chunks in a book project into a data frame
# chunks.R
# Parse the R code chunks in a Quarto/knitr book project into a data frame,
# one row per chunk, so you can audit code-folding, find figure chunks that
# still need treatment, spot old-style chunk labels, etc. See "Mind Your
# Chunks" (friendly.github.io) for the full writeup.
#
# Usage: run from the project root. Adapt `chapter_files` / `child_files`
# below to match your own project's file-naming convention -- as written,
# this assumes chapter source files are named like "04-something.qmd" in
# the working directory, with any knitr child documents listed explicitly.
@friendly
friendly / knit_include.R
Created March 14, 2026 21:33
knit_include(): pipe-friendly helper to render gt tables and htmlwidgets in any knitr output format (HTML, PDF, Word)
#' Include an HTML-renderable object in any knitr output format
#'
#' A pipe-friendly helper in Rmarkdown, Quarto, and other documents for objects that render natively in HTML output
#' (RStudio Viewer, HTML documents) but need a convenient image fallback for PDF, Word,
#' and other non-HTML formats. It was designed to solve a problem in rendering the result of `color_table()`,
#' and then generalized to provide for other similar cases.
#'
#' Supported classes (handled internally) solve this problem by saving the image to a file and then inserting it in the document
#' using `knitr::include_graphics()`:
#' - `"gt_tbl"` (gt package) -- saved via \code{\link[gt]{gtsave}}
@friendly
friendly / R_birthday_card.R
Created March 2, 2026 03:02
R Birthday card generated by Claude Sonnet 4.6
# Happy Birthday, R!
# R 1.0.0 was first released on February 29, 2000
# Celebrating 26 years of statistical computing!
out_file <- "r_birthday_cake.png"
png(out_file, width = 800, height = 660, bg = "#FFF0F5")
par(mar = c(0.3, 0.3, 0.3, 0.3), bg = "#FFF0F5")
plot(0, 0, type = "n", xlim = c(0, 10), ylim = c(0, 10),
axes = FALSE, xlab = "", ylab = "")
rect(-1, -1, 11, 11, col = "#FFF0F5", border = NA)
@friendly
friendly / fix_roxygen_items.R
Created December 9, 2025 22:36
Functions to fix one of the awful things that `Rd2roxygen::Rd2roxygen()` does when converting a package
#!/usr/bin/env Rscript
# Functions to fix one of the awful things that `Rd2roxygen::Rd2roxygen()` does when it tries to convert
# a package to use roxygen documentation
#
# * Fixes roxygen2 \item formatting used in \describe{} blocks to document arguments and elements of returned results
# * Changes \item{list("VarName")} to \item{\code{VarName}}
#
# Usage:
# From R: source("fix_roxygen_items.R"); fix_roxygen_items("R/data.R")
@friendly
friendly / determinant_distribition.R
Created November 17, 2025 02:08
Distribution of Determinants of 3×3 Matrices
---
title: "Distribution of Determinants of 3×3 Matrices from {1,2,...,9}"
author: "Michael Friendly"
date: "`r Sys.Date()`"
output:
html_document:
toc: true
toc_float: true
code_folding: show
theme: united
@friendly
friendly / discrim-demo.R
Created November 15, 2025 03:05
demonstration plots for discriminant analysis
#' ---
#' title: discriminant analysis demo
#' author: Michael Friendly
#' ---
library(MASS)
library(ggplot2)
#library(candisc)
library(dplyr)
@friendly
friendly / colorize.R
Last active July 20, 2025 22:29
Render text in color for Markdown / Quarto | Format refs to packages in inline text
#' Render text in color for Markdown / Quarto documents using LaTeX or CSS styles
#'
#' This function uses `\textcolor{}{}` from the `xcolor` package for LaTeX output
#' or a CSS `<span>` for HTML output.
#'
#' Note that a color not defined in the `xcolor` package will trigger a latex error.
#' e.g., `darkgreen` is not defined but can use:
#' \definecolor{darkgreen}{RGB}{1,50,32}
#'
#' @param text Text to display, a character string
@friendly
friendly / penguin-colors.R
Created August 10, 2024 02:50
R functions for consistent sets of colors for penguins examples
#' ---
#' title: penguin colors
#' author: Michael Friendly
#' ---
# Create consistent sets of colors for penguins examples
# These specific colors are taken from https://github.com/srvanderplas/ggpcp-paper/blob/main/index.R
# See the image at: https://allisonhorst.github.io/palmerpenguins/reference/figures/lter_penguins.png
peng.colors <- function(shade=c("medium", "light", "dark")) {