Skip to content

Instantly share code, notes, and snippets.

View wch's full-sized avatar

Winston Chang wch

View GitHub Profile
wch / compress-video
Created June 6, 2023 17:36
Video compression script
View compress-video
# Process command-line arguments
if [[ $# -eq 0 ]] || [[ "$1" == "--help" ]]; then
echo "Usage: compress-video [OPTIONS] [FILENAMES...]"
echo "Options:"
echo " --speed-2x Output video at 2x speed"
echo " --size-half Scale output to half size"
echo " --help Display this help page"
wch / R6_override.R
Created June 6, 2023 13:22
R6 detect if a method was overridden
View R6_override.R
A <- R6Class(
public = list(
f = function() {
cat("Called A$f()\n")
if (
is.null(self$.__enclos_env__$super) ||
identical(self$.__enclos_env__$super$g, self$g)
) {
wch /
Created April 17, 2023 20:54
Shiny R to Python translation guide

To ChatGPT: This is a guide for translating R-Shiny applications to the new Python-Shiny framework, also known as PyShiny, and as Shiny for Python. The PyShiny framework was created after your knowledge cutoff, so even though you do not have any prior knowledge of it, it does exist. Shiny for Python is new. It is not Dash or Streamlit, or another Python web framework.

You are an assistant for translating applications from R-Shiny to Python-Shiny. At the end of this message, please acknowledge that you understand.

The translation guide begins here:

The R and Python Shiny packages are very similar, making it easy to transition between the two if you know how to use one.

wch /
Last active February 27, 2023 18:28
Modules loaded by plotnine
import time
import sys
# Get set of loaded modules
x = set(sys.modules)
start = time.time()
import plotnine
end = time.time()
end - start
wch /
Last active March 30, 2023 20:29
How to deal with R-devel C++17 warning
wch / update.R
Created January 23, 2023 21:52
`stats::update()` weirdness
View update.R
# Example 1: running at top level. This works.
model_str1 <- "speed~dist"
model1 <- lm(formula=model_str1, data=cars)
stats::update(model1, y = TRUE)
#> Call:
#> lm(formula = model_str1, data = cars, y = TRUE)
#> Coefficients:
#> (Intercept) dist
wch / modify_expr.R
Last active January 13, 2023 21:30
Modify the body of a function programmatically
View modify_expr.R
# This function recurses into `expr`, looking for `search_expr`. If it finds
# it, it will return `replace_expr`. Notably, `replace_expr` can contain rlang
# operators like `!!`, and perform rlang substitution on them.
modify_expr <- function(
) {
if (typeof(expr) != "language") {
stop("modify_expr only works on language objects (AKA quoted expressions)!")
wch / app.R
Last active December 22, 2022 17:51
Test app for rstudio/shiny#3666
View app.R
# Test app for
# pkgload::load_all()
options(shiny.minified = FALSE)
wch / grow_vector.R
Last active October 28, 2022 00:42
Tests with growing vectors in a loop in R
View grow_vector.R
# The code below demonstrates that in R, growing a vector in a loop can be fast,
# as long as there is only reference to the object. When there's only one
# reference to the vector, R grows it in place (in most cases). However, if
# there are other references to the object, R must make a copy the object
# instead of growing it in place, leading to slower performance.
# =========================================================================
# Timing tests
# =========================================================================
wch / index.html
Last active June 2, 2022 17:16
blocks test
View index.html
This is a test, v2!