Skip to content

Instantly share code, notes, and snippets.

View nanxstats's full-sized avatar

Nan Xiao nanxstats

View GitHub Profile
@nanxstats
nanxstats / floating-equal.R
Created November 20, 2025 18:16
Compare two floating point number equivalence testing methods
library(testthat)
# Method A: round to 6 significant digits, then compare with default tiny tolerance
equal_after_signif <- function(x, y) {
expect_equal(signif(x, digits = 6), signif(y, digits = 6))
}
# Method B: compare with relative/absolute tolerance = 1e-6
equal_with_tolerance <- function(x, y) {
expect_equal(x, y, tolerance = 1e-6)

Update toolchain

  • rustup self update
  • rustup update stable
  • cargo update
  • cargo fetch

Code style

  • cargo fmt

Prepare

  • npm install
  • Make changes to source code
  • Run formatter and sort imports
  • Run tests (development workflow section in AGENTS.md)
  • Run manual tests (manual testing section in AGENTS.md)

Manifest

@nanxstats
nanxstats / py-release-checklist.md
Last active October 12, 2025 04:52
Simple Release checklist for Python packages

Update environment

Code style

  • python scripts/verify_ascii.py
@nanxstats
nanxstats / iosevka-subset.sh
Created May 24, 2025 23:40
Subset Iosevka font for smaller file size
# <https://fdp.io/blog/2024-10-08-choosing-a-font/>
uv init fontsub
uv add fonttools
uv add brotli
pyftsubset "Iosevka-Extended.woff2" \
--output-file="Iosevka-Extended-subset.woff2" \
--flavor="woff2" \
--layout-features="*" \
@nanxstats
nanxstats / foreach-rbind-benchmark.R
Last active March 25, 2025 15:53
Benchmark code comparing the performance of .combine = "rbind" vs. manual aggregation using data.table::rbindlist() in parallel foreach loops in R. Demonstrates significant speed improvements with manual aggregation.
library(doFuture)
plan(multisession, workers = 32)
options(scipen = 999)
anysvd <- function(id, dim = 10, nrep = 300) {
results <- vector("list", nrep)
for (j in 1:nrep) {
@nanxstats
nanxstats / cleanup-rproj-projectid.R
Last active February 10, 2025 15:01
Remove ProjectId field added by RStudio from .Rproj files at R startup
# Remove ProjectID from .Rproj files if freshly added
local({
xfun <- requireNamespace("xfun", quietly = TRUE)
rproj_files <- list.files(pattern = "\\.Rproj$", full.names = TRUE)
if (!xfun || length(rproj_files) == 0L) return(invisible(NULL))
lapply(rproj_files, function(f) {
diff_cmd <- system(paste("git diff --", shQuote(f)), intern = TRUE)
diff_out <- tryCatch(diff_cmd, error = function(e) character(0))
@nanxstats
nanxstats / changelog-gpt.R
Created August 29, 2024 05:12
Minimal example for analyzing R package changelogs with OpenAI API
desc <- tempfile()
curl::curl_download("https://cran.r-project.org/web/packages/tidyverse/DESCRIPTION", destfile = desc)
deps <- desc::desc_get_deps(desc)
pkgs <- deps[deps$type %in% c("Imports"), "package"]
urls <- paste0("https://cloud.r-project.org/web/packages/", pkgs, "/news/news.html")
html <- vector("list", length(urls))
for (i in seq_along(urls)) html[[i]] <- rawToChar(curl::curl_fetch_memory(urls[i])$content)
markdown <- vector("list", length(urls))
for (i in seq_along(urls)) markdown[[i]] <- paste0(pandoc::pandoc_convert(text = html[[i]], from = "html", to = "markdown"), collapse = "\n")
markdown <- markdown[-which(sapply(markdown, grepl, pattern = "# Not Found"))]
@nanxstats
nanxstats / gsDesign2-r-hub-v2.md
Created August 12, 2024 02:02
Check gsDesign2 with R-hub v2 GitHub Actions workflows
  1. Fork the repo and clone the repo to local.
  2. Remove all existing tests and GitHub Action workflows.
  3. Add Yihui's example as a test. Wrap the last step n - 454 in an expect_indetical() call and set the comparison to condition to 0.
  4. Run rhub::rhub_setup() to add the R-hub v2 workflow.
  5. Add, commit, and push the above changes.
  6. Run rhub::rhub_check() to run R-hub checks and select options to run the workflow. You might be prompted to set up a GitHub PAT locally and need to do that.
@nanxstats
nanxstats / simtrial-10k.tsv
Last active November 16, 2023 06:19
simtrial backend benchmark sketch
n 1 2 4 8 16
dplyr 5093.77 2671.44 1447.21 810.42 446.06
data.table 1336.79 677.94 364.5 217.75 143.95