Skip to content

Instantly share code, notes, and snippets.

View wch's full-sized avatar

Winston Chang wch

View GitHub Profile
@wch
wch / DESCRIPTION
Last active April 21, 2024 17:52
Including HTML, text, and Markdown files example for R Shiny
Type: Shiny
Title: Including HTML, text, and Markdown files
License: MIT
Author: Winston Chang <winston@rstudio.com>
AuthorUrl: http://www.rstudio.com/
Tags: include
DisplayMode: Showcase
@wch
wch / app.R
Last active April 18, 2024 08:42
Example Shiny app that automatically installs a source package when deployed to shinyapps.io
# By default, the directories in .libPaths() aren't writable on shinyapps.io, so
# create a subdir where we'll install our package.
if (!file.exists("R-lib")) {
dir.create("R-lib")
}
# Unfortunately, there's no way to get deployapp() to ignore this directory, so
# make sure to remove it locally before you call deployapp(). This can be done
# with:
# unlink("pkgInst/R-lib", recursive = TRUE)
@wch
wch / 00test.js
Last active April 9, 2024 14:42
Attempt to run sass with QuickJS
import * as sass from "./sass.default.js";
console.log(sass.info);
console.log(1);
const res = sass.compileString(`
.box {
width: 10px + 15px;
}
`)
console.log(2);
@wch
wch / compress-video
Created June 6, 2023 17:36
Video compression script
#!/bin/bash
# Process command-line arguments
if [[ $# -eq 0 ]] || [[ "$1" == "--help" ]]; then
echo "Usage: compress-video [OPTIONS] [FILENAMES...]"
echo
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
wch / demo.r
Last active March 25, 2024 13:02
mcmapply demonstration
m <- matrix(1:12, nrow=3)
# Double every element
# ===================
# Method 1: nested for loop
for(i in seq_len(nrow(m))) {
for(j in seq_len(ncol(m))) {
m2[i, j] <- m[i, j] * 2
}
@wch
wch / collision.R
Created February 16, 2024 14:35
Calculate collision probabilities
# Calculate the probability of a collision if `n` items are randomly drawn (with
# replacement) from a set with `total` number of items.
collision_prob <- function(n, total) {
prob_no_collision <- 1
# Do this in a loop instead of prod(numerators)/prod(denominators), because
# that method is prone to result in a loss of precision due to rounding.
for (i in seq(0, n-1)) {
prob_no_collision <- prob_no_collision * (total - i) / total
}
1 - prob_no_collision
@wch
wch / translation.md
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
wch / grow_vector.R
Last active December 24, 2023 17:33
Tests with growing vectors in a loop in 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
wch / multi_dispatch.R
Last active December 24, 2023 17:20
Multiple dispatch in R without S4
# ---- Multiple dispatch functions -----
multi_dispatch <- function(gen_name) {
calling_env <- parent.frame()
parent_call <- sys.call(sys.parent())
calling_fun <- sys.function(sys.parent())
arg1 <- eval(parent_call[[2]], calling_env)
arg2 <- eval(parent_call[[3]], calling_env)
@wch
wch / app.r
Last active December 18, 2023 16:41
Shiny example app with dynamic number of plots
max_plots <- 5
ui <- fluidPage(
headerPanel("Dynamic number of plots"),
sidebarPanel(
sliderInput("n", "Number of plots", value=1, min=1, max=5)
),