Permalink
Browse files

Add isolate function for accessing reactives non-reactively

  • Loading branch information...
1 parent 6c76b04 commit e2925c585fb28c890ea6c7fa625997477c8ceffd @jcheng5 jcheng5 committed Jan 3, 2013
Showing with 96 additions and 0 deletions.
  1. +1 −0 NAMESPACE
  2. +45 −0 R/reactives.R
  3. +50 −0 man/isolate.Rd
View
1 NAMESPACE
@@ -28,6 +28,7 @@ export(includeHTML)
export(includeMarkdown)
export(includeText)
export(invalidateLater)
+export(isolate)
export(mainPanel)
export(numericInput)
export(observe)
View
45 R/reactives.R
@@ -392,3 +392,48 @@ invalidateLater <- function(millis) {
})
invisible()
}
+
+#' Create a non-reactive scope for an expression
+#'
+#' Executes the given expression in a scope where reactive values or functions
+#' can be read, but they cannot cause the reactive scope of the caller to be
+#' re-evaluated when they change.
+#'
+#' Ordinarily, the simple act of reading a reactive value causes a relationship
+#' to be established between the caller and the reactive value, where a change
+#' to the reactive value will cause the caller to re-execute. (The same applies
+#' for the act of getting a reactive function's value.) The \code{isolate}
+#' function lets you read a reactive value or function without establishing this
+#' relationship.
+#'
+#' @param expr An expression that can access reactive values or functions.
+#'
+#' @examples
+#' \dontrun{
+#' observer(function() {
+#' input$saveButton # Do take a dependency on input$saveButton
+#'
+#' # isolate a simple expression
+#' data <- get(isolate(input$dataset)) # No dependency on input$dataset
+#' writeToDatabase(data)
+#' })
+#'
+#' observer(function() {
+#' input$saveButton # Do take a dependency on input$saveButton
+#'
+#' # isolate a whole block
+#' data <- isolate({
+#' a <- input$valueA # No dependency on input$valueA or input$valueB
+#' b <- input$valueB
+#' c(a=a, b=b)
+#' })
+#' writeToDatabase(data)
+#' })
+#' }
+#' @export
+isolate <- function(expr) {
+ ctx <- Context$new()
+ ctx$run(function() {
+ eval.parent(expr)
+ })
+}
View
50 man/isolate.Rd
@@ -0,0 +1,50 @@
+\name{isolate}
+\alias{isolate}
+\title{Create a non-reactive scope for an expression}
+\usage{
+ isolate(expr)
+}
+\arguments{
+ \item{expr}{An expression that can access reactive values
+ or functions.}
+}
+\description{
+ Executes the given expression in a scope where reactive
+ values or functions can be read, but they cannot cause
+ the reactive scope of the caller to be re-evaluated when
+ they change.
+}
+\details{
+ Ordinarily, the simple act of reading a reactive value
+ causes a relationship to be established between the
+ caller and the reactive value, where a change to the
+ reactive value will cause the caller to re-execute. (The
+ same applies for the act of getting a reactive function's
+ value.) The \code{isolate} function lets you read a
+ reactive value or function without establishing this
+ relationship.
+}
+\examples{
+\dontrun{
+observer(function() {
+ input$saveButton # Do take a dependency on input$saveButton
+
+ # isolate a simple expression
+ data <- get(isolate(input$dataset)) # No dependency on input$dataset
+ writeToDatabase(data)
+})
+
+observer(function() {
+ input$saveButton # Do take a dependency on input$saveButton
+
+ # isolate a whole block
+ data <- isolate({
+ a <- input$valueA # No dependency on input$valueA or input$valueB
+ b <- input$valueB
+ c(a=a, b=b)
+ })
+ writeToDatabase(data)
+})
+}
+}
+

0 comments on commit e2925c5

Please sign in to comment.