| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/shinywrappers.R, R/utils-lang.R |
| 3 |
\name{createRenderFunction} |
| 4 |
\alias{createRenderFunction} |
| 5 |
\alias{quoToFunction} |
| 6 |
\alias{installExprFunction} |
| 7 |
\title{Implement custom render functions} |
| 8 |
\usage{ |
| 9 |
createRenderFunction( |
| 10 |
func, |
| 11 |
transform = function(value, session, name, ...) value, |
| 12 |
outputFunc = NULL, |
| 13 |
outputArgs = NULL, |
| 14 |
cacheHint = "auto", |
| 15 |
cacheWriteHook = NULL, |
| 16 |
cacheReadHook = NULL |
| 17 |
) |
| 18 |
|
| 19 |
quoToFunction(q, label = sys.call(-1)[[1]], ..stacktraceon = FALSE) |
| 20 |
|
| 21 |
installExprFunction( |
| 22 |
expr, |
| 23 |
name, |
| 24 |
eval.env = parent.frame(2), |
| 25 |
quoted = FALSE, |
| 26 |
assign.env = parent.frame(1), |
| 27 |
label = sys.call(-1)[[1]], |
| 28 |
wrappedWithLabel = TRUE, |
| 29 |
..stacktraceon = FALSE |
| 30 |
) |
| 31 |
} |
| 32 |
\arguments{ |
| 33 |
\item{func}{A function without parameters, that returns user data. If the |
| 34 |
returned value is a promise, then the render function will proceed in async |
| 35 |
mode.} |
| 36 |
|
| 37 |
\item{transform}{A function that takes four arguments: \code{value}, |
| 38 |
\code{session}, \code{name}, and \code{...} (for future-proofing). This |
| 39 |
function will be invoked each time a value is returned from \code{func}, |
| 40 |
and is responsible for changing the value into a JSON-ready value to be |
| 41 |
JSON-encoded and sent to the browser.} |
| 42 |
|
| 43 |
\item{outputFunc}{The UI function that is used (or most commonly used) with |
| 44 |
this render function. This can be used in R Markdown documents to create |
| 45 |
complete output widgets out of just the render function.} |
| 46 |
|
| 47 |
\item{outputArgs}{A list of arguments to pass to the \code{uiFunc}. Render |
| 48 |
functions should include \code{outputArgs = list()} in their own parameter list, |
| 49 |
and pass through the value to \code{markRenderFunction}, to allow app authors to |
| 50 |
customize outputs. (Currently, this is only supported for dynamically |
| 51 |
generated UIs, such as those created by Shiny code snippets embedded in R |
| 52 |
Markdown documents).} |
| 53 |
|
| 54 |
\item{cacheHint}{One of \code{"auto"}, \code{FALSE}, or some other information to |
| 55 |
identify this instance for caching using \code{\link[=bindCache]{bindCache()}}. If \code{"auto"}, it |
| 56 |
will try to automatically infer caching information. If \code{FALSE}, do not |
| 57 |
allow caching for the object. Some render functions (such as \link{renderPlot}) |
| 58 |
contain internal state that makes them unsuitable for caching.} |
| 59 |
|
| 60 |
\item{cacheWriteHook}{Used if the render function is passed to \code{bindCache()}. |
| 61 |
This is an optional callback function to invoke before saving the value |
| 62 |
from the render function to the cache. This function must accept one |
| 63 |
argument, the value returned from \code{renderFunc}, and should return the value |
| 64 |
to store in the cache.} |
| 65 |
|
| 66 |
\item{cacheReadHook}{Used if the render function is passed to \code{bindCache()}. |
| 67 |
This is an optional callback function to invoke after reading a value from |
| 68 |
the cache (if there is a cache hit). The function will be passed one |
| 69 |
argument, the value retrieved from the cache. This can be useful when some |
| 70 |
side effect needs to occur for a render function to behave correctly. For |
| 71 |
example, some render functions call \code{\link[=createWebDependency]{createWebDependency()}} so that Shiny |
| 72 |
is able to serve JS and CSS resources.} |
| 73 |
|
| 74 |
\item{q}{Quosure of the expression \code{x}. When capturing expressions to create |
| 75 |
your quosure, it is recommended to use \code{\link[rlang:defusing-advanced]{rlang::enquo0()}} to not unquote |
| 76 |
the object too early. See \code{\link[rlang:defusing-advanced]{rlang::enquo0()}} for more details.} |
| 77 |
|
| 78 |
\item{label}{A label for the object to be shown in the debugger. Defaults to |
| 79 |
the name of the calling function.} |
| 80 |
|
| 81 |
\item{expr}{A quoted or unquoted expression, or a quosure.} |
| 82 |
|
| 83 |
\item{name}{The name the function should be given} |
| 84 |
|
| 85 |
\item{eval.env}{The desired environment for the function. Defaults to the |
| 86 |
calling environment two steps back.} |
| 87 |
|
| 88 |
\item{quoted}{Is the expression quoted?} |
| 89 |
|
| 90 |
\item{assign.env}{The environment in which the function should be assigned.} |
| 91 |
|
| 92 |
\item{wrappedWithLabel, ..stacktraceon}{Advanced use only. For stack manipulation purposes; see |
| 93 |
\code{\link[=stacktrace]{stacktrace()}}.} |
| 94 |
} |
| 95 |
\value{ |
| 96 |
An annotated render function, ready to be assigned to an |
| 97 |
\code{output} slot. |
| 98 |
} |
| 99 |
\description{ |
| 100 |
Developer-facing utilities for implementing a custom \code{renderXXX()} function. |
| 101 |
Before using these utilities directly, consider using the \href{http://www.htmlwidgets.org/develop_intro.html}{\code{htmlwidgets} package} to implement custom |
| 102 |
outputs (i.e., custom \code{renderXXX()}/\code{xxxOutput()} functions). That said, |
| 103 |
these utilities can be used more directly if a full-blown htmlwidget isn't |
| 104 |
needed and/or the user-supplied reactive expression needs to be wrapped in |
| 105 |
additional call(s). |
| 106 |
} |
| 107 |
\details{ |
| 108 |
To implement a custom \code{renderXXX()} function, essentially 2 things are needed: |
| 109 |
\enumerate{ |
| 110 |
\item Capture the user's reactive expression as a function. |
| 111 |
\itemize{ |
| 112 |
\item New \code{renderXXX()} functions can use \code{quoToFunction()} for this, but |
| 113 |
already existing \code{renderXXX()} functions that contain \code{env} and \code{quoted} |
| 114 |
parameters may want to continue using \code{installExprFunction()} for better |
| 115 |
legacy support (see examples). |
| 116 |
} |
| 117 |
\item Flag the resulting function (from 1) as a Shiny rendering function and |
| 118 |
also provide a UI container for displaying the result of the rendering |
| 119 |
function. |
| 120 |
\itemize{ |
| 121 |
\item \code{createRenderFunction()} is currently recommended (instead of |
| 122 |
\code{\link[=markRenderFunction]{markRenderFunction()}}) for this step (see examples). |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
\section{Functions}{ |
| 127 |
\itemize{ |
| 128 |
\item \code{quoToFunction()}: convert a quosure to a function. |
| 129 |
|
| 130 |
\item \code{installExprFunction()}: converts a user's reactive \code{expr} into a |
| 131 |
function that's assigned to a \code{name} in the \code{assign.env}. |
| 132 |
|
| 133 |
}} |
| 134 |
\examples{ |
| 135 |
# A custom render function that repeats the supplied value 3 times |
| 136 |
renderTriple <- function(expr) { |
| 137 |
# Wrap user-supplied reactive expression into a function |
| 138 |
func <- quoToFunction(rlang::enquo0(expr)) |
| 139 |
|
| 140 |
createRenderFunction( |
| 141 |
func, |
| 142 |
transform = function(value, session, name, ...) { |
| 143 |
paste(rep(value, 3), collapse=", ") |
| 144 |
}, |
| 145 |
outputFunc = textOutput |
| 146 |
) |
| 147 |
} |
| 148 |
|
| 149 |
# For better legacy support, consider using installExprFunction() over quoToFunction() |
| 150 |
renderTripleLegacy <- function(expr, env = parent.frame(), quoted = FALSE) { |
| 151 |
func <- installExprFunction(expr, "func", env, quoted) |
| 152 |
|
| 153 |
createRenderFunction( |
| 154 |
func, |
| 155 |
transform = function(value, session, name, ...) { |
| 156 |
paste(rep(value, 3), collapse=", ") |
| 157 |
}, |
| 158 |
outputFunc = textOutput |
| 159 |
) |
| 160 |
} |
| 161 |
|
| 162 |
# Test render function from the console |
| 163 |
reactiveConsole(TRUE) |
| 164 |
|
| 165 |
v <- reactiveVal("basic") |
| 166 |
r <- renderTriple({ v() }) |
| 167 |
r() |
| 168 |
#> [1] "basic, basic, basic" |
| 169 |
|
| 170 |
# User can supply quoted code via rlang::quo(). Note that evaluation of the |
| 171 |
# expression happens when r2() is invoked, not when r2 is created. |
| 172 |
q <- rlang::quo({ v() }) |
| 173 |
r2 <- rlang::inject(renderTriple(!!q)) |
| 174 |
v("rlang") |
| 175 |
r2() |
| 176 |
#> [1] "rlang, rlang, rlang" |
| 177 |
|
| 178 |
# Supplying quoted code without rlang::quo() requires installExprFunction() |
| 179 |
expr <- quote({ v() }) |
| 180 |
r3 <- renderTripleLegacy(expr, quoted = TRUE) |
| 181 |
v("legacy") |
| 182 |
r3() |
| 183 |
#> [1] "legacy, legacy, legacy" |
| 184 |
|
| 185 |
# The legacy approach also supports with quosures (env is ignored in this case) |
| 186 |
q <- rlang::quo({ v() }) |
| 187 |
r4 <- renderTripleLegacy(q, quoted = TRUE) |
| 188 |
v("legacy-rlang") |
| 189 |
r4() |
| 190 |
#> [1] "legacy-rlang, legacy-rlang, legacy-rlang" |
| 191 |
|
| 192 |
# Turn off reactivity in the console |
| 193 |
reactiveConsole(FALSE) |
| 194 |
|
| 195 |
} |