Skip to content

Instantly share code, notes, and snippets.

View jcheng5's full-sized avatar

Joe Cheng jcheng5

View GitHub Profile
@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
@jcheng5
jcheng5 / app.R
Created July 5, 2019 15:35
Celsius <=> Fahrenheit
library(shiny)
ui <- fluidPage(
numericInput("temp_c", "Celsius", NA),
numericInput("temp_f", "Fahrenheit", NA)
)
server <- function(input, output, session) {
c_to_f <- function(c, decimals = 1) {
round((c * 9 / 5) + 32, decimals)
# BE SURE TO set your Mapbox access token with:
# options(mapbox.accessToken = "...")
library(htmltools)
library(leaflet)
mapboxgl_deps <- list(
htmlDependency(
"mapbox-gl-js", "0.53.0", c(href = "https://api.mapbox.com/mapbox-gl-js/v0.53.0/"),
script = "mapbox-gl.js",
num_suffix <- function(x, base = 1000, suffixes = c("K", "M", "B", "T")) {
if (length(suffixes) == 0) {
tibble(
scale_by = rep_len(1, length(x)),
suffix = rep_len("", length(x))
)
}
i <- floor(log(abs(x), base = base))
@jcheng5
jcheng5 / addDynamicTiles.R
Created January 19, 2019 16:38
Leaflet dynamic tiles
library(shiny)
library(leaflet)
#' Add a tile layer whose source is an R function
#'
#' @param tileFunc A function(x, y, z) that returns a 256x256 image object
#' suitable for passing to `png::writePNG`.
#' @seealso [leaflet::addTiles()] for other parameters
addDynamicTiles <- function(map, tileFunc,
layerId = paste0("leafletRaster", sample.int(9999999, 1)),
@jcheng5
jcheng5 / 1-original.R
Created November 9, 2018 16:32
Simple Shiny app, with and without plot caching
library(ggplot2)
library(shiny)
ui <- fluidPage(
varSelectInput("color_by", "Color by:", diamonds, selected = "cut"),
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
@jcheng5
jcheng5 / .bashrc
Created October 31, 2018 17:46
Launch Shiny apps from command line
# Examples:
# shiny # Launch the Shiny app in the current directory
# shiny path/to/app # Launch the Shiny app at the specified path
# shiny . launch.browser=F,port=8000 # Launch Shiny in the current dir with given options
function shiny {
R --vanilla -q -e "shiny::runApp(\"${1:-.}\", launch.browser=TRUE, $2)"
}
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)