Skip to content

shinyRenderWidget: Add support for caching #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: htmlwidgets
Type: Package
Title: HTML Widgets for R
Version: 1.5.2.9000
Version: 1.5.2.9001
Authors@R: c(
person("Ramnath", "Vaidyanathan", role = c("aut", "cph")),
person("Yihui", "Xie", role = c("aut")),
Expand Down
26 changes: 18 additions & 8 deletions R/htmlwidgets.R
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ createWidget <- function(name,
#' @param env The environment in which to evaluate \code{expr}.
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
#' is useful if you want to save an expression in a variable.
#' @param cacheHint Extra information to use for optional caching using
#' \code{\link[shiny]{bindCache}}.
#'
#' @return An output or render function that enables the use of the widget
#' within Shiny applications.
Expand Down Expand Up @@ -425,11 +427,10 @@ shinyWidgetOutput <- function(outputId, name, width, height, package = name,

#' @rdname htmlwidgets-shiny
#' @export
shinyRenderWidget <- function(expr, outputFunction, env, quoted) {

shinyRenderWidget <- function(expr, outputFunction, env, quoted, cacheHint = "auto") {
checkShinyVersion()
# generate a function for the expression
func <- shiny::exprToFunction(expr, env, quoted)
shiny::installExprFunction(expr, "func", env, quoted)

renderWidget <- function(instance) {
if (!is.null(instance$elementId)) {
Expand Down Expand Up @@ -463,19 +464,28 @@ shinyRenderWidget <- function(expr, outputFunction, env, quoted) {
toJSON(payload)
}

if (!is.null(asNamespace("shiny")$createRenderFunction)) {
# The cacheHint arg is not present in Shiny < 1.6.0.
if ("cacheHint" %in% names(formals(shiny::createRenderFunction))) {
shiny::createRenderFunction(
func,
function(instance, session, name, ...) {
renderWidget(instance)
},
outputFunction, NULL
outputFunction,
NULL,
cacheHint = cacheHint
)
} else {
shiny::markRenderFunction(outputFunction, function() {
renderWidget(func())
})
shiny::createRenderFunction(
func,
function(instance, session, name, ...) {
renderWidget(instance)
},
outputFunction,
NULL
)
}

}

checkShinyVersion <- function(error = TRUE) {
Expand Down
5 changes: 4 additions & 1 deletion man/htmlwidgets-shiny.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.