Files
shiny/man/quoToFunction.Rd
Barret Schloerke 8b74338b0f Add sustainEnvAndQuoted(). Remove getQuosure() (#3468)
Documentation to come in a later PR

Co-authored-by: Barret Schloerke <schloerke@gmail.com>
Co-authored-by: Winston Chang <winston@stdout.org>
2021-07-26 17:54:37 -04:00

120 lines
4.3 KiB
R

% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils-lang.R
\name{quoToFunction}
\alias{quoToFunction}
\alias{sustainEnvAndQuoted}
\title{Convert a quosure to a function for a Shiny render function}
\usage{
quoToFunction(q, label = sys.call(-1)[[1]], ..stacktraceon = FALSE)
sustainEnvAndQuoted(q, x, env, quoted)
}
\arguments{
\item{q}{A quosure.}
\item{label}{A label for the object to be shown in the debugger. Defaults to
the name of the calling function.}
\item{..stacktraceon}{Advanced use only. For stack manipulation purposes; see
\code{\link[=stacktrace]{stacktrace()}}.}
\item{x}{An expression or quosure.}
\item{env}{An environment. This is provided for backward compatibility.}
\item{quoted}{A boolean indicating whether or not \code{env} is quoted. This is
provided for backward compatibility.}
}
\description{
This takes a quosure and label, and wraps them into a function that should be
passed to \code{\link[=createRenderFunction]{createRenderFunction()}} or \code{\link[=markRenderFunction]{markRenderFunction()}}.
\code{handleEnvAndQuoted()} and \code{quoToFunction()} are meant to be used together in a
\code{render} function, to capture user expressions or quosures and convert them
to functions. They are meant to replace the older functions
\code{\link[=installExprFunction]{installExprFunction()}} and \code{\link[=exprToFunction]{exprToFunction()}} (although those will continue
to work in the future). See the examples in \code{\link[=installExprFunction]{installExprFunction()}} for
information on how to migrate to \code{getQuosure()} and \code{quoToFunction()}.
}
\details{
This function was added in Shiny 1.6.0. Previously, it was recommended to use
\code{\link[=installExprFunction]{installExprFunction()}} or \code{\link[=exprToFunction]{exprToFunction()}} in render functions, but now we
recommend using \code{\link[=quoToFunction]{quoToFunction()}}, because it does not require \code{env} and
\code{quoted} arguments -- that information is captured by quosures provided by
\pkg{rlang}.
Although \code{getQuosure()} can take \code{env} and \code{quoted} parameters, it is
recommended that they not be used, except for backward compatibility.
The recommended usage of \code{getQuosure()} and \code{quoToFunction()} does not
include use of the \code{env} and \code{quoted} parameters. If it is necessary to
use quoted expressions and/or custom environments for evaluating, it can be
done with quosures and \code{\link[rlang:inject]{rlang::inject()}}. The examples below demonstrate how
to do this.
If you are updating from \code{\link[=installExprFunction]{installExprFunction()}} or \code{\link[=exprToFunction]{exprToFunction()}} to
these functions, see the examples in the documentation for the old functions
for how to migrate them.
}
\examples{
# Example of a new renderer, similar to renderText.
# This is something that toolkit authors will do.
renderTriple <- function(expr) {
# Convert expr to a quosure, and then to a function
func <- quoToFunction(rlang::enquo0(expr))
# Wrap up func, with another function which takes the value of func()
# and modifies it.
createRenderFunction(
func,
transform = function(value, session, name, ...) {
paste(rep(value, 3), collapse=", ")
},
# The outputFunc can be used by rmarkdown shiny apps to automatically
# generate outputs.
outputFunc = textOutput
)
}
# 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"
# If you want to use a quoted expression, use rlang:inject().
a <- 1
expr <- quote({ values$A })
tripleA <- rlang::inject(renderTriple(!!expr))
isolate(tripleA())
# "text, text, text"
# Capturing an expression and an environment, using a quosure and rlang::inject():
e <- new.env()
e$vals <- reactiveValues(A="hello")
# Create a quosure that captures both the expression and environment.
myquo <- rlang::new_quosure(quote({ vals$A }), env = e)
tripleA <- rlang::inject(renderTriple(!!myquo))
isolate(tripleA())
# "hello, hello, hello"
}
\seealso{
\code{\link[=createRenderFunction]{createRenderFunction()}} for example usage.
}