Skip to content

Instantly share code, notes, and snippets.

View MichaelChirico's full-sized avatar

Michael Chirico MichaelChirico

View GitHub Profile
@MichaelChirico
MichaelChirico / cran_readchar_args.R
Created January 26, 2024 15:24
readChar() usage on CRAN
library(jsonlite)
library(data.table)
library(lintr)
read_page <- function(page) {
tmp <- tempfile()
on.exit(unlink(tmp))
system2("curl",
c("--location", "--request",
"GET", sprintf("'https://api.github.com/search/code?q=readChar+org:cran+language:R+-path:.Rd&per_page=100&page=%d'", page),
@MichaelChirico
MichaelChirico / gha_results.R
Last active November 17, 2023 16:47
GHA results
library(jsonlite)
library(data.table)
headers <- shQuote(c(
"-H", "Accept: application/vnd.github+json",
"-H", sprintf("Authorization: Bearer %s", Sys.getenv("GITHUB_PAT")),
"-H", "X-GitHub-Api-Version: 2022-11-28"
))
url_fmt <- "https://api.github.com/repos/r-lib/lintr/actions/runs?per_page=%d&page=%d"
@MichaelChirico
MichaelChirico / gist:7706fe44cf29b5e12007bf4e1c1f73ce
Created November 14, 2023 00:49
grep() slower than which(grepl())??
library(microbenchmark)
N = 1e4
v = sample(letters, N, TRUE)
microbenchmark(times = 200L, grep("a", v), which(grepl("a", v)), grep("[a-m]", v), which(grepl("[a-m]", v)), grep("[a-z]", v), which(grepl("[a-z]", v)))
# Unit: microseconds
# expr min lq mean median uq max neval cld
# grep("a", v) 630.561 640.4110 670.5257 655.4915 680.6115 866.132 200 b
# which(grepl("a", v)) 598.802 609.5220 637.0542 621.0965 652.9120 839.231 200 a
# grep("[a-m]", v) 692.601 708.4215 735.6788 721.3065 744.6365 1207.722 200 d
@MichaelChirico
MichaelChirico / explicit_return_linter.R
Created November 10, 2023 18:18
explicit_return_linter XPath
allowed_functions <- c(
# Normal calls
"return", "stop", "warning", "message", "stopifnot", "q", "quit",
"invokeRestart", "tryInvokeRestart",
# Normal calls from non-default libraries
"LOG", "abort",
# tests in the RUnit framework are functions ending with a call to one
# of the below. would rather users just use a different framework
@MichaelChirico
MichaelChirico / test-explicit_return_linter.R
Created November 10, 2023 18:13
explicit_return_linter tests
test_that("explicit_return_linter works in simple function", {
lines <- c(
"foo <- function(bar) {",
" return(bar)",
"}"
)
expect_lint(lines, NULL, explicit_return_linter())
})
test_that("explicit_return_linter works for using stop() instead of returning", {
@MichaelChirico
MichaelChirico / marriage_tax_2022.R
Created October 15, 2023 05:33
Marriage tax 2022
library(data.table)
library(rvest)
library(xml2)
brackets = "https://taxfoundation.org/data/all/federal/2022-tax-brackets/" |>
read_html() |>
xml_find_all("//table[contains(caption/text(), '2022 Federal Income')]") |>
html_table()
brackets = as.data.table(brackets[[1L]])[-.N]
@MichaelChirico
MichaelChirico / nyt_xword.R
Last active September 14, 2023 06:30
EDA on my New York Times Crossword data
library(data.table)
# output from https://github.com/mattdodge/nyt-crossword-stats
# python fetch_puzzle_stats.py -u "..." -p "..." -s 1993-11-21
DT = fread("data.csv")
# Sunday is a bit of its own thing, so put it last
DT[, day := factor(day, levels = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"))]
DT[, iso_week := format(date, "%G-%V")]
@MichaelChirico
MichaelChirico / check_examples.R
Created August 1, 2023 16:37
Run package examples, allowing for possibly multiple failures
#' Run a package's examples, allowing tolerance for failures
#'
#' @noRd
.RunExamples <- function(man_dir, timeout = 60) {
# It appears the result order is not stable; sort to ensure this
rd_files <- sort(dir(man_dir, full.names = TRUE, pattern = "\\.[Rr]d$"))
if (length(rd_files) == 0L) {
cat("No examples to run.\n")
return(list())
@MichaelChirico
MichaelChirico / cmf_2023_citations.R
Created July 7, 2023 16:15
Citations count in CA Math Framework
# As downloaded 2023-07-06
library(data.table)
library(xml2)
# NB: a .docx is "just" a .zip directory
unzip("mathfwappendixbsbe.docx", exdir=tempdir())
doc_xml = read_xml(file.path(tempdir(), "word", "document.xml"))
@MichaelChirico
MichaelChirico / api_output.xml
Created June 22, 2023 18:38
XML output from SF Transit API with puzzling behavior

<?xml version="1.0" encoding="iso-8859-1"?>
<siri:Siri xsi:schemaLocation="http://www.siri.org.uk/siri http://www.kizoom.com/standards/netex/schema/0.99.1/xsd/NeTEx_siri.xsd"
xmlns:siri="http://www.siri.org.uk/siri"
xmlns="http://www.netex.org.uk/netex"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gml="http://www.opengis.net/gml" version="1.0">
<siri:ServiceDelivery>
<siri:ResponseTimestamp>2023-06-21T23:27:30-07:00</siri:ResponseTimestamp>
<DataObjectDelivery>