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.R
Last active February 3, 2024 17:34
Using OAuth2 with Shiny
library(shiny)
# WARNING: This sketch does not make proper use of the "state" parameter.
# Doing so usually involves using cookies, which can be done with the
# Rook package but I have not done that here. If you choose to use this
# approach in production, please check the state parameter properly!
APP_URL <- if (interactive()) {
# This might be useful for local development. If not, just hardcode APP_URL
# to the deployed URL that you'll provide a few lines below.
@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 / 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)
@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 / ggbrush.R
Last active September 9, 2023 23:55
ggbrush
library(ggplot2)
library(shiny)
# Call ggbrush with a ggplot2 object, and the dimensions which
# should be brushed (try "xy" for scatter, "x" for histogram).
# The plot will show in RStudio Viewer or your web browser, and
# any observations selected by the user will be returned.
ggbrush <- function(plotExpr, direction = c("xy", "x", "y")) {
# See below for definition of dialogPage function
@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 / withConsoleRedirect.R
Created June 29, 2016 05:05
Someone at #useR2016 asked me if you can have Shiny execute code in observers/reactives but send the console output to the browser.
library(shiny)
withConsoleRedirect <- function(containerId, expr) {
# Change type="output" to type="message" to catch stderr
# (messages, warnings, and errors) instead of stdout.
txt <- capture.output(results <- expr, type = "output")
if (length(txt) > 0) {
insertUI(paste0("#", containerId), where = "beforeEnd",
ui = paste0(txt, "\n", collapse = "")
)
@jcheng5
jcheng5 / README.md
Last active December 15, 2022 16:01
Using arbitrary Leaflet plugins with Leaflet for R

Using arbitrary Leaflet JS plugins with Leaflet for R

The Leaflet JS mapping library has lots of plugins available. The Leaflet package for R provides direct support for some, but far from all, of these plugins, by providing R functions for invoking the plugins.

If you as an R user find yourself wanting to use a Leaflet plugin that isn't directly supported in the R package, you can use the technique shown here to load the plugin yourself and invoke it using JS code.

@jcheng5
jcheng5 / global.R
Last active November 30, 2022 02:01 — forked from dgrapov/global.R
#initialize
library(datasets)
library(ggplot2)
#helper function (convert vector to named list)
namel<-function (vec){
tmp<-as.list(vec)
names(tmp)<-as.character(unlist(vec))
tmp
}