Skip to content

Instantly share code, notes, and snippets.

@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
}
@nanxstats
nanxstats / shiny-bs5-switches.R
Created April 14, 2022 04:26
Bootstrap 5 Switches for Shiny
switchInput <- function(inputId, label, value = FALSE, disabled = FALSE, width = NULL) {
value <- shiny::restoreInput(id = inputId, default = value)
inputTag <- htmltools::tags$input(id = inputId, type = "checkbox", role = "switch", class = "form-check-input")
if (!is.null(value) && value) {
inputTag$attribs$checked <- NA
}
if (!is.null(disabled) && disabled) {
inputTag$attribs$disabled <- NA
}
htmltools::tags$div(
@nanxstats
nanxstats / dt-news-list.R
Created April 9, 2022 07:08
Paginated news list with DT/DataTables
# Generate mock data -----------------------------------------------------------
set.seed(42)
k <- 100
df <- data.frame(
title = unlist(purrr::map2(
.x = stringr::word(stringi::stri_rand_lipsum(k), start = 1, end = 10),
.y = rep("#", k),
.f = function(.x, .y) as.character(htmltools::tags$a(.x, href = .y))
)),
time = as.POSIXct(unlist(
@nanxstats
nanxstats / tnum.R
Last active January 19, 2022 05:42
Test native font stack on tabular numbers
library(knitr)
library(htmltools)
library(webshot2)
df <- mtcars[2:7, c("mpg", "disp", "drat", "wt", "qsec")]
rownames(df) <- NULL
colnames(df) <- NULL
tnum <- function(font, title, output) {
tags$html(