Skip to content

Instantly share code, notes, and snippets.

View jcheng5's full-sized avatar

Joe Cheng jcheng5

View GitHub Profile
@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
}
@jcheng5
jcheng5 / filters.R
Last active September 29, 2022 13:08
library(shiny)
columnFilterUI <- function(id) {
ns <- NS(id)
uiOutput(ns("filter_container"))
}
columnFilter <- function(input, output, session, df, col_num, choice_filter) {
# This renders a selectInput and only re-renders when the selected data
# frame changes. (i.e. it doesn't re-render when filters change state.)
@jcheng5
jcheng5 / create_forked_task.R
Last active August 3, 2022 17:11
Concurrent, forked, cancellable tasks in Shiny
library(shiny)
# Also uses parallel, shinyjs, tools
# Create a long-running task, executed in a forked process. (Doesn't work on Windows)
#
# The return value is a promise-like object with three
# methods:
# - completed(): FALSE initially, then TRUE if the task succeeds,
# fails, or is cancelled. Reactive, so when the state changes
# any reactive readers will invalidate.
@jcheng5
jcheng5 / server.R
Created November 10, 2012 08:13
Shiny file upload demo
shinyServer(function(input, output) {
output$filetable <- reactiveTable(function() {
if (is.null(input$files)) {
# User has not uploaded a file yet
return(NULL)
}
input$files
})
})
@jcheng5
jcheng5 / server.R
Created November 10, 2012 23:33
Adding raw HTML to Shiny apps
# Nothing to see here
shinyServer(function(input, output) {
})
@jcheng5
jcheng5 / README.md
Last active January 6, 2022 21:27
Shiny without reactivity

Shiny without reactivity

In order to contrast Shiny's reactive programming model with the imperative programming techniques of traditional UI programming, here's a demonstration of what it looks like when you take Shiny's UI libraries and HTTP/websocket IO model, but remove the reactive programming aspects of it.

The original application code is here. In comparison, this strawman code is:

  • More verbose. The server function is more than twice as long.
  • Less efficient. During startup, the updateSelectedData() function runs twice, and updateClusters() and updatePlot() each run three times. Only one time each should be needed, which is what happens with the reactive version.
  • Less maintainable. When a particular input changes, the question of which update functions need to be called, and in what order, is critical for correctness. In non-trivial apps this is very difficult to
@jcheng5
jcheng5 / global.R
Last active December 20, 2021 14:32 — forked from dempseydata/global.R
###############################################
##
## Attempt no 2 at building a shiny web app
## for AB Testing use - using global.R
##
## global.R - loading and defining variables for the global environment
##
###############################################
# Pallette used in some charts as a general indicator color for better or worse that the control group
@jcheng5
jcheng5 / debounce.R
Last active October 20, 2021 04:54
reactive debounce for Shiny
# Returns a reactive that debounces the given expression by the given time in
# milliseconds.
#
# This is not a true debounce in that it will not prevent \code{expr} from being
# called many times (in fact it may be called more times than usual), but
# rather, the reactive invalidation signal that is produced by expr is debounced
# instead. This means that this function should be used when \code{expr} is
# cheap but the things it will trigger (outputs and reactives that use
# \code{expr}) are expensive.
debounce <- function(expr, millis, env = parent.frame(), quoted = FALSE,
@jcheng5
jcheng5 / server.R
Last active June 17, 2021 18:37
Shiny example: Diamonds Explorer
library(shiny)
library(ggplot2)
function(input, output) {
dataset <- reactive({
diamonds[sample(nrow(diamonds), input$sampleSize),]
})
output$plot <- renderPlot({
@jcheng5
jcheng5 / server.r
Created October 29, 2012 06:21 — forked from wch/server.r
Experiments with shiny and ggplot2
library(shiny)
library(datasets)
library(ggplot2)
tg <- ToothGrowth
tg$dose <- factor(tg$dose)
# Define server logic
shinyServer(function(input, output) {