Skip to content

Instantly share code, notes, and snippets.

@johndrummond
johndrummond / lookup_list_vs_env.R
Created April 8, 2019 14:16
Simple comparison of performance of lookups of environments vs lists in R
# lookup_list_vs_env.R
# comparing the performance of lookups with lists as opposed to
# environments in R. As the size of the collection increases, the better
# performance of the environments for lookups is multiples better.
library(microbenchmark)
library(purrr)
library(stringr)
e1 <- new.env()
l1 <- list()
@johndrummond
johndrummond / shinyobserve.R
Last active April 24, 2019 18:19
simple example of closures and a future in a an shiny observer - polled updates as an alternative to promises
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
@johndrummond
johndrummond / gist:0bbc846da698e0b7e84bff9d2871ae0b
Created May 2, 2019 12:03
compare observeEvent to observe plus isolate in a shiny app
# oberveEvent includes the ability of the first parameter to be an expression. But if the return value of that expression is
# null, the ignore null setting blocks updates, even though other parts of the expression are updating.
# in the example below with the default settings, button Event1 will only trigger the update of "observeEvent either button"
# after Event2 has been pushed at least once
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
@johndrummond
johndrummond / shiny_invalidatelater_future.R
Created May 8, 2019 13:01
scheduled long running tasks in shiny with futures
library(shiny)
library(DT)
library(dplyr)
library(future)
library(magrittr)
plan(multiprocess)
dedupe <- function(r) {
makeReactiveBinding("val")
observe(val <<- r(), priority = 10)
@johndrummond
johndrummond / shinypromise.R
Created May 8, 2019 23:13
shiny app with long running calculation using promises
# notice the flush on the single user holds up the user updating, as the promise and reactive variable is there per user
library(shiny)
library(DT)
library(dplyr)
library(future)
library(promises)
plan(multiprocess)
ui <- fluidPage(
@johndrummond
johndrummond / shinyglobalpromise.R
Created May 9, 2019 09:13
long running task in shiny with task in global future
library(shiny)
library(DT)
library(dplyr)
library(future)
library(promises)
plan(multiprocess)
ui <- fluidPage(
verbatimTextOutput("timer"),
DTOutput("myTable")
@johndrummond
johndrummond / settingReactiveValTrigger
Created May 13, 2019 11:48
code showing that an observer that sets a reactive value, isn't invalidated by the reactive value being set elsewhere
#
#
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Reactive values updating"),
@johndrummond
johndrummond / shiny_dummy_json_health.R
Created August 5, 2019 22:44
demo shiny server app with health handler returning plain json on health path
#
# This is a Shiny web application.
# with a handler that returns plain json
# run and in a browser look for /health/
# e.g. http://127.0.0.1:5732/health/
# from https://stackoverflow.com/users/1455889/amaurel
# on https://stackoverflow.com/questions/19991654/shiny-server-print-json-as-a-result-output
# You can run the application by clicking
# the 'Run App' button above.
#
@johndrummond
johndrummond / shiny.router.dummy.R
Created August 5, 2019 22:50
sample shiny router app returning json but wrapped in websocket and html
# This was an attempt to return json on /health/ with shiny.router
# https://appsilon.com/shiny-router-package/
# trying https://stackoverflow.com/users/7268834/slavakx 's suggestion
# https://stackoverflow.com/questions/19991654/shiny-server-print-json-as-a-result-output
# but the json is already wrapped
library(shiny)
library(shiny.router)
library(data.table)
library(jsonlite)
# samples of some joins of data.tables with trivial tables
library(data.table)
dt1 <- data.table(f1=1:5, f2=sample(letters,5))
dt2 <- data.table(f1=2:6, f3=sample(letters,5))
dt3 <- data.table(f1=c(2,2:6), f3=sample(letters,6))
dt1
dt2
dt3