diff --git a/R/react.R b/R/react.R index 61e040f47..ca47a0e7f 100644 --- a/R/react.R +++ b/R/react.R @@ -55,6 +55,15 @@ Map <- setRefClass( ) ) +`[.Map` <- function(map, name) { + map$get(name) +} + +`[<-.Map` <- function(map, name, value) { + map$set(name, value) + return(map) +} + as.list.Map <- function(map) { sapply(map$keys(), map$get, @@ -207,6 +216,24 @@ Values <- setRefClass( ) ) +`[.Values` <- function(values, name) { + values$get(name) +} + +`[<-.Values` <- function(values, name, value) { + values$set(name, value) + return(values) +} + +make.values.accessor <- function(values) { + acc <- list(impl=values) + class(acc) <- 'ValuesAccessor' + acc +} +`$.ValuesAccessor` <- function(acc, name) { + acc[['impl']]$get(name) +} + Observable <- setRefClass( 'Observable', fields = c( @@ -259,6 +286,10 @@ Observable <- setRefClass( ) ) +observable <- function(func) { + Observable$new(func) +} + Observer <- setRefClass( 'Observer', fields = list( diff --git a/R/shiny.R b/R/shiny.R index 5666eea76..5e27ebcb0 100644 --- a/R/shiny.R +++ b/R/shiny.R @@ -157,6 +157,7 @@ start.app <- function(app, www.root, sys.www.root=NULL, port=8101L) { get.input <- function(name) { shinyapp$session$get(name) } + input <- make.values.accessor(shinyapp$session) if (is.function(app)) app() diff --git a/examples/02_hash/app.R b/examples/02_hash/app.R index 67ace2b47..27d4cd39d 100644 --- a/examples/02_hash/app.R +++ b/examples/02_hash/app.R @@ -1,15 +1,15 @@ library(digest) -input <- Observable$new(function() { - str <- get.input('input1') - if (get.input('addnewline')) +text <- observable(function() { + str <- input$input1 + if (input$addnewline) str <- paste(str, "\n", sep='') return(str) }) define.output('md5_hash', function() { - digest(input$get.value(), algo='md5', serialize=F) + digest(text$get.value(), algo='md5', serialize=F) }) define.output('sha1_hash', function() { - digest(input$get.value(), algo='sha1', serialize=F) + digest(text$get.value(), algo='sha1', serialize=F) }) \ No newline at end of file diff --git a/examples/03_distributions/app.R b/examples/03_distributions/app.R index 51f193804..9ba2fbb72 100644 --- a/examples/03_distributions/app.R +++ b/examples/03_distributions/app.R @@ -1,6 +1,6 @@ -data <- Observable$new(function() { +data <- observable(function() { # Choose a distribution function - dist <- switch(get.input('dist'), + dist <- switch(input$dist, norm = rnorm, unif = runif, lnorm = rlnorm, @@ -8,12 +8,12 @@ data <- Observable$new(function() { rnorm) # Generate n values from the distribution function - dist(as.integer(get.input('n'))) + dist(as.integer(input$n)) }) define.plot('plot1', function() { - dist <- get.input('dist') - n <- get.input('n') + dist <- input$dist + n <- input$n hist(data$get.value(), main=paste('r', dist, '(', n, ')', sep='')) @@ -25,4 +25,4 @@ define.table('table1', function() { define.output('summary1', function() { paste(capture.output(print(summary(data$get.value()))), collapse="\n") -}) \ No newline at end of file +})