Skip to content

Commit

Permalink
export and add docs for installExprFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcphers committed Oct 8, 2013
1 parent 6c7d9de commit d10cbc9
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export(includeHTML)
export(includeMarkdown)
export(includeScript)
export(includeText)
export(installExprFunction)
export(invalidateLater)
export(is.reactive)
export(is.reactivevalues)
Expand Down
10 changes: 9 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ exprToFunction <- function(expr, env=parent.frame(2), quoted=FALSE,
#' Installs an expression in the given environment as a function, and registers
#' debug hooks so that breakpoints may be set in the function.
#'
#' @note Wraps \code{exprToFunction}; see that method's documentation for
#' Can replace \code{exprToFunction} as follows:
#'
#' Before: \code{func <- exprToFunction(expr)}
#'
#' After: \code{installExprFunction(expr, "func")}
#'
#' @seealso Wraps \code{exprToFunction}; see that method's documentation for
#' more documentation and examples.
#'
#' @param expr A quoted or unquoted expression
Expand All @@ -210,6 +216,8 @@ exprToFunction <- function(expr, env=parent.frame(2), quoted=FALSE,
#' @param assign.env The environment in which the function should be assigned.
#' @param label A label for the object to be shown in the debugger. Defaults
#' to the name of the calling function.
#'
#' @export
installExprFunction <- function(expr, name, eval.env = parent.frame(2),
quoted = FALSE,
assign.env = parent.frame(1),
Expand Down
11 changes: 10 additions & 1 deletion man/exprToFunction.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
\title{Convert an expression or quoted expression to a function}
\usage{
exprToFunction(expr, env = parent.frame(2),
quoted = FALSE)
quoted = FALSE, caller_offset = 1)
}
\arguments{
\item{expr}{A quoted or unquoted expression, or a
Expand All @@ -13,6 +13,9 @@
Defaults to the calling environment two steps back.}

\item{quoted}{Is the expression quoted?}

\item{caller_offset}{The offset in the callstack of the function to be
considered the caller. Defaults to the direct caller.}
}
\description{
This is to be called from another function, because it
Expand All @@ -27,6 +30,12 @@
this will quote the original expression and convert it to
a function.
}
\note{
exprToFunction does not set debug hooks in the function it creates, so it is
not possible to set breakpoints in the original expressions and hit them during
Shiny application runtime. Use \code{\link{installExprFunction}} instead if you
need debug support.
}
\examples{
# Example of a new renderer, similar to renderText
# This is something that toolkit authors will do
Expand Down
70 changes: 70 additions & 0 deletions man/installExprFunction.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
\name{installExprFunction}
\alias{installExprFunction}
\title{Convert an expression to a function and place it in an environment}
\description{This is to be called from another function, because it will attempt
to get an unquoted expression from two calls back.}
\usage{
installExprFunction(expr, name, eval.env = parent.frame(2),
quoted = FALSE, assign.env = parent.frame(1),
label = as.character(sys.call(-1)[[1]]))
}
\arguments{
\item{expr}{A quoted or unquoted expression.}

\item{name}{The name of the function object to create.}

\item{eval.env}{The desired environment for the function. Defaults to the
calling environment two steps back.}

\item{quoted}{Is the expression quoted?}

\item{assign.env}{The environment in which to place the function object.
Defaults to the calling environment.}

\item{label}{A descriptive label for the function to be shown in the
debugger, if active. Defaults to the name of the calling function.}
}
\details{
Converts expr to a function, using the semantics described in
\code{\link{exprToFunction}}. Installs the newly created function into an
environment (the environment of the caller unless otherwise specified), and
registers debug hooks on the function object if a debugger is active so that
breakpoints may be set in it.
}
\seealso{
\code{link{exprToFunction}}
}
\examples{
# Example of a new renderer, similar to renderText
# This is something that toolkit authors will do
renderTriple <- function(expr, env=parent.frame(), quoted=FALSE) {
# Create a function named "func" from the expression
shiny::installExprFunction(expr, "func", env, quoted)

function() {
# Call the function just created
value <- func()
paste(rep(value, 3), collapse=", ")
}
}

# Example of using the renderer.
# This is something that app authors will do.
values <- reactiveValues(A="text")

\dontrun{
# Create an output object
output$tripleA <- renderTriple({
values$A
})
}

# At the R console, you can experiment with the renderer using isolate()
tripleA <- renderTriple({
values$A
})

isolate(tripleA())
# "text, text, text"
}

0 comments on commit d10cbc9

Please sign in to comment.