Skip to content

Instantly share code, notes, and snippets.

View nanxstats's full-sized avatar

Nan Xiao nanxstats

View GitHub Profile
@nanxstats
nanxstats / simtrial-10k.tsv
Last active November 16, 2023 06:19
simtrial backend benchmark sketch
View simtrial-10k.tsv
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]
View safe-rlang-eval-tidy.Rmd
# 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
View sdv-r6.R
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
View use_release_checklist.R
#' 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
View responsive-ggplot2.R
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 October 18, 2023 02:42
Convert MLModern from Type 1 format to OTF and WOFF2
View mlmodern-otf-woff2.sh
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
View error-handler.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/
View check-url.R
#' 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,
@nanxstats
nanxstats / superlative.Rmd
Created December 13, 2022 03:43
Click to change to a random superlative using JavaScript
View superlative.Rmd
---
title: "Click to change to a random superlative"
output: html_document
---
From <https://www.garrickadenbuie.com/blog/countdown-v0.4.0/>.
I'm <span class="superlative">overjoyed</span> to announce that...
```{css}
@nanxstats
nanxstats / gsDesign-gource.sh
Created October 28, 2022 02:13
Shell commands to generate version control visualization video for gsDesign using gource
View gsDesign-gource.sh
# Clone repo
git clone https://github.com/keaven/gsDesign.git
cd gsDesign
# Run gource - this will generate a 411GB ppm file
gource -3840x2160 --seconds-per-day 0.1 --auto-skip-seconds 0.01 --file-idle-time 0 --font-size 34 --key --logo man/figures/logo.png -o gsDesign.ppm
# Convert ppm to mp4
ffmpeg -y -r 60 -f image2pipe -vcodec ppm -i gsDesign.ppm -vcodec libx264 -preset medium -pix_fmt yuv420p -crf 1 -threads 0 -bf 0 gsDesign.mp4
# Merge audio to video
ffmpeg -i gsDesign.mp4 -i music.mp3 -c:v copy -c:a aac output.mp4
# Recommended by YouTube