Skip to content

Instantly share code, notes, and snippets.

View nanxstats's full-sized avatar

Nan Xiao nanxstats

View GitHub Profile
@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,
@nanxstats
nanxstats / superlative.Rmd
Created December 13, 2022 03:43
Click to change to a random superlative using JavaScript
---
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
# 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
@nanxstats
nanxstats / robotjs-update-extension.js
Last active June 22, 2023 06:49
Update unpacked extension with RobotJS
// Automate browser extension updates with RobotJS
const robot = require("robotjs");
const { execSync } = require("child_process");
const updateExtension = (coordRemove) => {
// Open in extensions page in browser
execSync('open -a "Google Chrome" chrome://extensions');
// Move to and click "Remove" button of the extension
robot.setMouseDelay(1000);
@nanxstats
nanxstats / sparse-index-tracking-msaenet.R
Last active May 31, 2023 05:30
Sparse index tracking with a two-stage procedure using {msaenet} and {CVXR}
# Load data and split training/test set ----------------------------------------
library("xts")
index2010 <- sparseIndexTracking::INDEX_2010
x_tr <- index2010$X[1:126]
x_te <- index2010$X[127:252]
r_tr <- index2010$SP500[1:126]
r_te <- index2010$SP500[127:252]
@nanxstats
nanxstats / chromote-print-to-pdf.R
Last active August 20, 2022 04:10
Print HTML to PDF using chromote
library("promises")
library("chromote")
#' Print HTML to PDF using chromote
#'
#' @param url Input URL
#' @param filename Output file name
#' @param wait_ If TRUE, run in synchronous mode,
#' otherwise, run in asynchronous mode.
#' @param ... Additional parameters for Page.printToPDF, see
@nanxstats
nanxstats / txtvsbin.R
Last active June 15, 2022 21:11
Find out if a file is plain text or binary using the zlib algorithm
# <https://github.com/madler/zlib/blob/8678871f18f4dd51101a9db1e37791f975969079/doc/txtvsbin.txt>
#' Classify any file into text file or binary file
#'
#' @param path File path.
#' @param n The (maximal) number of bytes to read.
#'
#' @return Logical. `TRUE` if text, `FALSE` if binary.
#'
#' @examples
@nanxstats
nanxstats / foreach-expr-sketch.R
Last active June 9, 2022 14:22
foreach wrapper function that parallelizes any user-supplied expressions
# Get operator (parallel or serial)
# Idea from <https://github.com/tidymodels/tune/blob/c4a1d891ac77e64086a417ab75ad822178cd87ee/R/parallel.R>
get_operator <- function() {
is_par <- foreach::getDoParWorkers() > 1
if (is_par) {
res <- foreach::`%dopar%`
} else {
res <- foreach::`%do%`
}
res
@nanxstats
nanxstats / shiny-file-input-area.R
Created April 18, 2022 01:50
Shiny file input area (Bootstrap 5)
fileInputArea <- function(inputId, label, multiple = FALSE, accept = NULL,
width = NULL, buttonLabel = "Browse...", placeholder = "No file selected") {
restoredValue <- restoreInput(id = inputId, default = NULL)
# Catch potential edge case - ensure that it's either NULL or a data frame.
if (!is.null(restoredValue) && !is.data.frame(restoredValue)) {
warning("Restored value for ", inputId, " has incorrect format.")
restoredValue <- NULL
}