Skip to content

Instantly share code, notes, and snippets.

@jjallaire
jjallaire / server.R
Created December 18, 2013 13:08
Shiny Example 07_widgets
library(shiny)
library(datasets)
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
# Return the requested dataset
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
@jjallaire
jjallaire / server.R
Created December 18, 2013 13:06
Shiny Example 05_sliders
library(shiny)
# Define server logic for slider examples
shinyServer(function(input, output) {
# Reactive expression to compose a data frame containing all of the values
sliderValues <- reactive({
# Compose data frame
data.frame(
@jjallaire
jjallaire / server.R
Created December 18, 2013 13:04
Shiny Example 04_mpg
library(shiny)
library(datasets)
# We tweak the "am" field to have nicer factor labels. Since this doesn't
# rely on any user inputs we can do this once at startup and then use the
# value throughout the lifetime of the application
mpgData <- mtcars
mpgData$am <- factor(mpgData$am, labels = c("Automatic", "Manual"))
@jjallaire
jjallaire / server.R
Created December 18, 2013 13:03
Shiny Example 03_reactivity
library(shiny)
library(datasets)
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
# By declaring databaseInput as a reactive expression we ensure that:
#
# 1) It is only called when the inputs it depends on changes
# 2) The computation and result are shared by all the callers (it
@jjallaire
jjallaire / server.R
Created December 18, 2013 12:57
Shiny Example 02_text
library(shiny)
library(datasets)
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
# Return the requested dataset
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
@jjallaire
jjallaire / gist:4240240
Created December 8, 2012 13:14
loaded package requires update
loadedPackageRequiresUpdate <- function(pkgs) {
if (any(pkgs %in% loadedNamespaces())) {
return(TRUE)
}
else {
# compute dependent packages
avail <- available.packages()
if (getRversion() >= "2.15")
deps <- tools:::package_dependencies(pkgs, db = avail, recursive = TRUE)
else
@jjallaire
jjallaire / gist:4235475
Created December 7, 2012 18:50
package updates required
loadedPackageUpdateRequired <- function(pkgs) {
if (any(pkgs %in% loadedNamespaces())) {
return(TRUE)
}
else {
# compute dependent packages
avail <- available.packages()
if (getRversion() >= "2.15")
deps <- tools:::package_dependencies(pkgs, db = avail, recursive = TRUE)
@jjallaire
jjallaire / gist:4055396
Created November 11, 2012 16:18
table1
library(Rcpp)
library(microbenchmark)
cppFunction('
IntegerVector table1(const CharacterVector x) {
std::map<std::string, int> counts;
std::vector<std::string> vec = as<std::vector<std::string> >(x);
int n = x.length();
for (int i = 0; i < n; i++) {
@jjallaire
jjallaire / table.cpp
Created November 11, 2012 16:15 — forked from hadley/table.cpp
IntegerVector table1(const CharacterVector x) {
std::map<char*, int> counts;
int n = x.length();
for (int i = 0; i < n; i++) {
counts[x[i]]++;
}
// Loop through each element of map and output into named vector
IntegerVector out(counts.size());
@jjallaire
jjallaire / gist:4040462
Created November 8, 2012 18:02
RcppArmadillo example
// [[Rcpp::depends(RcppArmadillo)]]
#include <Rcpp.h>
#include <RcppArmadillo.h>
using namespace Rcpp;