Skip to content

Instantly share code, notes, and snippets.

View jcheng5's full-sized avatar

Joe Cheng jcheng5

View GitHub Profile
@jcheng5
jcheng5 / app.py
Created March 8, 2024 03:14
Color blindness helper in Shiny Express
# Live demo at https://shinylive.io/py/app/#code=NobwRAdghgtgpmAXGKAHVA6VBPMAaMAYwHsIAXOcpMAMwCdiYACABQEkAZJgSxlWLpkmbGFADmcADoRe-QUwgBXPtiZQAzgtTTZAoSQA2A9dnXTp9Rk3UALbhFW75dOFEJluANzh4mLgI4WDMy29tgYcAAeqC7qmk5C9qiKZL4uEAAmcHS+itzmStwYSSkA+jTcBnAAFJK0lVL4THUi4nBMFVV1AJTSecUQyWSl6gbcWXS1YDaKjb51ABKz1nY0ZHW+AAy+AEwArHtbvRAFAALpE1hG65lwNCvca3AZ1d2I0kyfTADETBzEUAyTDINnavDaHy+4IkTAAvMJRBIMMRUJRqgFqiUyBhOjVut1gJsALrAOoZKBkNAUmx1IndDAkCDeQTVADkACUAOIAIVZxy+zQgAt+AGFSMyhCCwYj2mRiAplDg1HQ6FBVDQBExRDJkgYKdxSJDPtC4KVBnCtBgoCq1ZiZfyvkaeDLSjZ1J4LYMrTbsNVgIZjKYMHQxAAjUpy13u6qobiROAGQlEgD0+0OTFj8cTAEYU2nfJmE8AdnmDt0OprCwYeEKTWbMLEbGgagBabO+ADM3TpBWFTAAyqshDMpEKoS63Z5gIgtkSLXa2lGpzOmMSmABqJiYwYpDAj15MZNMNP4pgAUiY2adTtF4uyQgW-YAakxQ24ANbA+Vc7lOush0NPUwa1VV9f1iCMOgTHUPd3QjYhSgAmM4yLYkCxQnMiXQrNizpCs6AzDCa2dRdJx7MdPhvJh2TgWxmzUTJrEIKAqiYRRUC-Y8Dj-F0AMQ2im1RIF4QXCRELDYMBObUTTS9OjUXLAAqLi9npDQyGwVFqi9PJyAADgdSiKJ+JgxSZe8FRUZVQNfD9OPYLhWgkHjF2IFIhgtJy4BxYIQNtf8w34+TnmOJ0XDIRQ6FrF03LIIZpDAABfIkgA
from PIL import Image
import nump
@jcheng5
jcheng5 / app.py
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
jcheng5 / app.R
Last active January 11, 2024 00:01
Extended task example app
library(shiny)
library(bslib)
library(future)
library(promises)
future::plan(future::multisession)
ui <- page_sidebar(
sidebar = sidebar(
numericInput("x", "x", 5),
@jcheng5
jcheng5 / README.md
Last active June 21, 2023 00:27
Debounce and throttle for Shiny for Python

Sketch of debounce/throttle decorators for Shiny for Python

Drop ratelimit.py into your app directory to use in your own Shiny for Python app. See app.py 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
jcheng5 / README.md
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:

library(shiny)
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
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");
shadowDiv.classList.add("gt-shadow-container");
containerEl.parentElement.insertBefore(shadowDiv, styleEl);
@jcheng5
jcheng5 / README.md
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: https://github.com/jeroen/solarisvm

@jcheng5
jcheng5 / gist:34ef026bdefd3c9b558920b9a9804c01
Last active July 25, 2019 09:44
Solaris 10 R-devel csw
590 pkgutil -i wget
603 pkgutil -i gcc5gfortran
609 pkgutil -i -y libiconv_dev
613 pkgutil -i -y zlib_dev
615 pkgutil -i -y libz_dev
620 pkgutil -i -y liblzma_dev
622 pkgutil -i -y libpcre_dev
624 pkgutil -i -y libcurl_dev
634 pkgutil -i -y libcurl4_dev
647 pkgutil -y -i gmake