Skip to content

Instantly share code, notes, and snippets.

View jcheng5's full-sized avatar

Joe Cheng jcheng5

View GitHub Profile
library(shiny)
bigvec <- paste0("a", 1:1e5)
named_bigvec <- setNames(bigvec, bigvec)
nested_biglist <- lapply(named_bigvec, function(item) setNames(list(item), item))
test_set <- list(
"Unnamed vector" = c(1, 2),
"Named vector" = c(a = 1, B = 2),
"Partially named vector" = c(a = 1, B = 2, 3),
@jcheng5
jcheng5 / README.md
Last active June 26, 2020 01:08
Using a more recent version of jQuery with Shiny

To opt into a more recent version of jQuery, you can add an htmlDependency object pointing to your desired version, somewhere (anywhere) in your UI:

htmltools::htmlDependency("jquery", "3.3.1",
  src = c(href = "https://code.jquery.com/"),
  script = "jquery-3.3.1.min.js")

This example will serve jQuery 3.3.1 from the jQuery CDN. If your app needs to work with clients that won't be able to connect to the wider internet, you'll need to download the jquery-3.3.1.min.js file, put it in an app subdirectory (say, jquery), and point to the directory using the src argument.

@jcheng5
jcheng5 / gist:abc401032b0012fc9d2d570617820d32
Last active August 31, 2018 17:19
Shiny modules outline
  1. Why use modules?
    • Reuse
    • Organization/wrangling complexity (small pieces reliably composed)
  2. Functions vs. modules
    • Namespacing Shiny inputs/outputs
  3. Passing values in/getting values out
    • Reactives by name vs. by value
    • ^ in both directions (arguments and return values)
  4. Connecting modules
  • How can module UI compose? ("higher-order" modules)
@jcheng5
jcheng5 / ldaptest.js
Last active July 3, 2018 18:45
LDAP test
// Before running this script, set the LDAP_PASSWORD environment variable in
// the shell. You can do this without echoing the password by running:
//
// read -s LDAP_PASSWORD && export LDAP_PASSWORD
//
// You'll also need to modify the variable definitions at the top of this file.
// == BEGIN configuration variables ===============
var url = "ldap://adtest.rstudio.com/dc=adtest,dc=rstudio,dc=com";
@jcheng5
jcheng5 / gist:1ff1efbc539542ecedde92f25458a872
Created June 8, 2018 23:36
Cancellable tasks (without async)
base_task_iterator <- function(should_continue, iter_body) {
if (should_continue()) {
iter_body()
later::later(~base_task_iterator(should_continue, iter_body))
}
invisible()
}
while_task_iterator <- function(cancelExpr, whileExpr, func) {
http://builder.r-hub.io/file/5e5a06074fba4784a9f164e3970a2ead
@jcheng5
jcheng5 / renderui_example.R
Created April 16, 2018 18:34
freezeReactiveValue examples
library(shiny)
library(ggplot2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("dataset", "Dataset", c("cars", "pressure", "mtcars")),
uiOutput("input_ui")
),
mainPanel(
@jcheng5
jcheng5 / dedupe.R
Created February 28, 2018 00:44
Lazy higher order reactives
library(shiny)
library(dplyr)
library(ggplot2)
dedupe <- function(r) {
x <- reactiveVal(isolate(r()))
o <- observe({
x(r())
})
@jcheng5
jcheng5 / shinymod.snippet
Last active January 22, 2019 14:09
RStudio snippet for Shiny module
snippet shinymod
${1:name}UI <- function(id) {
ns <- NS(id)
tagList(
${0}
)
}
${1:name} <- function(input, output, session) {

This snippet shows how you could use later to create an API that can be called on a background thread, to perform an arbitrary task on the main R thread, and return the result to the background thread. The background thread blocks until the result is returned from the main R thread.