View withConsoleRedirect.R
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 = "")
)
View app.R
library(shiny)
library(shinyjs)
library(DT)
data("cars")
ui <- tagList(
useShinyjs(),
navbarPage("OSD",
View README.md

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.

View add-sliders.R
library(shiny)
#slider module ------------------------
sliderUI <- function(id) {
ns <- NS(id)
sliderInput(ns("bins"), "Number of Bins:", min = 1, max = 5, value = 3)
}
slider <- function(input, output, session) {}
View app.R
library(shiny)
source("inputwrapper.R")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
numericInput("x", NULL, sample.int(1000, 1)),
actionButton("go", "Go")
),
View app1.R
# Simple example of exporting UI to another function (this works)
library(shiny)
tabUI <- function(id) {
ns <- NS(id)
fluidRow(
textOutput(ns("filter"))
)
}
View regret.R
RxValue <- function(initialValue = NULL) {
rv <- shiny::reactiveValues(value = initialValue)
# Call without an argument to read, or with an argument to write
function(value) {
if (missing(value))
rv$value
else
rv$value <- value
}
View comprehensions.R
lc <- function(expr, For = x, In, If = TRUE) {
expr <- substitute(expr)
For <- substitute(For)
subst <- list()
evalEnv <- parent.frame()
lapply(In, function(el) {
subst[[as.character(For)]] <- el
eval(substituteDirect(expr, subst), evalEnv)
View lift.R
library(shiny)
# lift0: a -> Signal a
lift0 <- function(a) {
reactive(a)
}
# lift1: (a -> b) -> Signal a -> Signal b
lift1 <- function(func) {
function(a) {
View README.md

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