Skip to content

Instantly share code, notes, and snippets.

View nanxstats's full-sized avatar

Nan Xiao nanxstats

View GitHub Profile
@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
@nanxstats
nanxstats / safe-rlang-eval-tidy.Rmd
Created June 30, 2023 17:15
Use `rlang::eval_tidy()` safely [DRAFT]
# Use `rlang::eval_tidy()` safely
```{r setup, include=FALSE}
knitr::opts_chunk$set(comment = "#>", collapse = TRUE, error = TRUE)
```
`rlang::eval_tidy()` is a powerful tool that allows you to evaluate R code
within a specific environment, often a data frame or a list.
However, if used carelessly with user input, it can introduce
security vulnerabilities.
@nanxstats
nanxstats / sdv-r6.R
Created June 27, 2023 17:23
Calling SDV in an R6 class
library(reticulate)
# Ignore this ----
use_python("/opt/homebrew/bin/python3.10")
# reticulate works ----
sdv <- import("sdv")
@nanxstats
nanxstats / use_release_checklist.R
Created June 12, 2023 03:41
Create R package release checklists without requiring project or version control context
#' Create a release checklist without requiring context
#'
#' @param package Package name.
#' @param version Release version number.
#' @param on_cran Is the package already on CRAN? Default is `TRUE`.
#' @param has_news Does the package use `NEWS.md`? Default is `TRUE`.
#' @param has_readme Does the package use `README.Rmd`? Default is `FALSE`.
#' @param has_lifecycle Does the package use lifecycle? Default is `FALSE`.
#'
#' @return Release checklist in Markdown format (invisibly).
@nanxstats
nanxstats / responsive-ggplot2.R
Created June 4, 2023 23:19
Responsive design for statistical graphics with Shiny and ggplot2
library("shiny")
library("ggplot2")
# Container max widths from Bootstrap 5
max_width <- c(540, 720, 960, 1140, 1320)
# Output image height scaling factors
height_scale <- c(6, 3, 3, 1.5, 1, 1)
# Given plot width, find the grid interval index while considering page columns
@nanxstats
nanxstats / mlmodern-otf-woff2.sh
Last active September 10, 2024 17:55
Convert MLModern from Type 1 format to OTF and WOFF2
brew install fontforge
brew install woff2
curl -L http://mirrors.ctan.org/fonts/mlmodern.zip > mlmodern.zip
unzip mlmodern.zip
cd mlmodern/type1/
fontforge -lang=ff -c 'Open($1); Generate($1:r + ".otf")' mlmr12.pfb
@nanxstats
nanxstats / error-handler.R
Last active January 18, 2023 21:06
Add custom error messages to specific errors in R
# Add custom error messages to specific errors in R
error_handler <- function() {
rlang::entrace()
e <- rlang::last_error()$message
if (grepl("there is no package called", x = e)) {
message("* This can be a custom error message with multiple lines.")
}
}
if ("rlang" %in% rownames(installed.packages())) {
@nanxstats
nanxstats / check-url.R
Last active January 23, 2024 09:37
A general-purpose link checker for R Markdown and Quarto projects https://nanx.me/blog/post/rmarkdown-quarto-link-checker/
#' Flatten copy
#'
#' @param from Source directory path.
#' @param to Destination directory path.
#'
#' @return Destination directory path.
#'
#' @details
#' Copy all `.Rmd`, `.qmd`, and `.md` files from source to destination,
#' rename the `.qmd` and `.md` files with an additional `.Rmd` extension,