Skip to content

Instantly share code, notes, and snippets.

View jcheng5's full-sized avatar

Joe Cheng jcheng5

View GitHub Profile
jcheng5 / app.R
Created April 5, 2024 18:34
crew + ExtendedTask while keeping track of who requested each result
run_task <- function(model, n, delay_secs) {
jcheng5 /
Created March 8, 2024 03:14
Color blindness helper in Shiny Express
# Live demo at
from PIL import Image
import nump
jcheng5 /
Last active February 2, 2024 16:24
Shiny for Python module for console you can write to
from shiny import App, reactive, render, ui, module
from shiny.session import Session
from contextlib import redirect_stdout, redirect_stderr
import asyncio
from shiny_console import Console, console_ui, console_server
# == Example usage =======================================
app_ui = ui.page_fluid(
jcheng5 / app.R
Last active January 11, 2024 00:01
Extended task example app
ui <- page_sidebar(
sidebar = sidebar(
numericInput("x", "x", 5),
jcheng5 /
Last active June 21, 2023 00:27
Debounce and throttle for Shiny for Python

Sketch of debounce/throttle decorators for Shiny for Python

Drop into your app directory to use in your own Shiny for Python app. See for an example of these decorators in action.

Why is this a gist instead of a PR? Because to implement this "properly" for Shiny for Python, this would need to be beefed up to include type annotations, support for async, and unit/integration tests, which would all be more effort than this has taken so far. (To be clear, we want to do all that, but in the meantime, here's this gist.)

jcheng5 /
Last active February 2, 2024 10:10
Accepting POST requests from Shiny

Accepting POST requests from Shiny

(This post was motivated by a talk by @jnolis at CascadiaRConf 2021)

Recent versions of Shiny have an undocumented feature for handling POST requests that are not associated with any specific Shiny session. (Note that this functionality is missing our normal level of polish; it's a fairly low-level hook that I needed to make some things possible, but doesn't make anything easy.)

In a nutshell, it works by replacing your traditional ui object with a function(req), and then marking that function with an attribute indicating that it knows how to handle both GET and POST:

assign("msg", local({
i <- 0
function(url, ...) {
i <<- i + 1
on.exit(i <<- i - 1)
if (i > 1) {
message("reentrancy detected!")
return("reentrancy detected!")
cat("") # Calls R_ProcessEvents()
jcheng5 / image_dimensions.R
Last active April 18, 2020 18:09
Determine file image size in R
jpeg_dimensions <- function(filename, bytes = 1024) {
bytes <- readBin(filename, "raw", n = bytes)
if (length(bytes) < 2 || bytes[[1]] != 0xFF || bytes[[2]] != 0xD8) {
stop("Couldn't decode jpeg")
ff <- which(bytes == 0xFF)
c0 <- which(bytes == 0xC0)
```{js echo=FALSE}
function enshadowGtTable(tableEl) {
var containerEl = tableEl.parentElement;
var styleEl = containerEl.previousElementSibling;
if (containerEl.tagName !== "DIV" || styleEl.tagName !== "STYLE") {
throw new Error("Unexpected document structure");
var shadowDiv = document.createElement("div");
containerEl.parentElement.insertBefore(shadowDiv, styleEl);
jcheng5 /
Last active March 10, 2021 16:02
Installing R-devel on Solaris 10 VM

As far as CRAN is concerned, there are two flavors of R on Solaris: one that is built using the Solaris Studio compiler, and one that is built using the GNU/gcc toolchain. The latter is far more up-to-date, but if your package requires it, then your DESCRIPTION file must declare that with the line SystemRequirements: GNU make.

These instructions are for configuring, building, and installing R-devel using the GNU/gcc toolchain (only).

You'll need VMWare Fusion on Mac, or VMWare Workstation (?) on Windows/Linux.

Get Solaris VM

Download the Solaris VM provided by Jeroen Ooms: