Skip to content

Commit

Permalink
Class output of reactive.
Browse files Browse the repository at this point in the history
Also add print method and test
  • Loading branch information
hadley committed Jul 27, 2013
1 parent 194d2f9 commit a5db7d0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ S3method(as.list,reactivevalues)
S3method(format,shiny.tag)
S3method(format,shiny.tag.list)
S3method(names,reactivevalues)
S3method(print,reactive)
S3method(print,shiny.tag)
S3method(print,shiny.tag.list)
export(HTML)
Expand Down Expand Up @@ -57,6 +58,7 @@ export(includeMarkdown)
export(includeScript)
export(includeText)
export(invalidateLater)
export(is.reactive)
export(isolate)
export(mainPanel)
export(numericInput)
Expand Down
17 changes: 15 additions & 2 deletions R/reactives.R
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,16 @@ Observable <- setRefClass(
#' See the \href{http://rstudio.github.com/shiny/tutorial/}{Shiny tutorial} for
#' more information about reactive expressions.
#'
#' @param x An expression (quoted or unquoted).
#' @param x For \code{reactive}, an expression (quoted or unquoted). For
#' \code{is.reactive}, an object to test.
#' @param env The parent environment for the reactive expression. By default, this
#' is the calling environment, the same as when defining an ordinary
#' non-reactive expression.
#' @param quoted Is the expression quoted? By default, this is \code{FALSE}.
#' This is useful when you want to use an expression that is stored in a
#' variable; to do so, it must be quoted with `quote()`.
#' @param label A label for the reactive expression, useful for debugging.
#' @return a function, wrapped in a S3 class "reactive"
#'
#' @examples
#' values <- reactiveValues(A=1)
Expand All @@ -403,9 +405,20 @@ reactive <- function(x, env = parent.frame(), quoted = FALSE, label = NULL) {
if (is.null(label))
label <- sprintf('reactive(%s)', paste(deparse(body(fun)), collapse='\n'))

Observable$new(fun, label)$getValue
o <- Observable$new(fun, label)
structure(o$getValue@.Data, observable = o, class = "reactive")
}

#' @S3method print reactive
print.reactive <- function(x, ...) {
label <- attr(x, "observable")$.label
cat(label, "\n")
}

#' @export
#' @rdname reactive
is.reactive <- function(x) inherits(x, "reactive")

# Return the number of times that a reactive expression or observer has been run
execCount <- function(x) {
if (is.function(x))
Expand Down
9 changes: 8 additions & 1 deletion man/reactive.Rd
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
\name{reactive}
\alias{is.reactive}
\alias{reactive}
\title{Create a reactive expression}
\usage{
reactive(x, env = parent.frame(), quoted = FALSE,
label = NULL)

is.reactive(x)
}
\arguments{
\item{x}{An expression (quoted or unquoted).}
\item{x}{For \code{reactive}, an expression (quoted or
unquoted). For \code{is.reactive}, an object to test.}

\item{env}{The parent environment for the reactive
expression. By default, this is the calling environment,
Expand All @@ -21,6 +25,9 @@
\item{label}{A label for the reactive expression, useful
for debugging.}
}
\value{
a function, wrapped in a S3 class "reactive"
}
\description{
Wraps a normal expression to create a reactive
expression. Conceptually, a reactive expression is a
Expand Down

0 comments on commit a5db7d0

Please sign in to comment.