Skip to content

Commit

Permalink
Merge pull request ramnathv#316 from ramnathv/async
Browse files Browse the repository at this point in the history
Support for Shiny async
  • Loading branch information
jjallaire authored Jun 5, 2018
2 parents e3097fa + 9137f0c commit 6ceb5b5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Imports:
yaml
Suggests:
knitr (>= 1.8)
Enhances: shiny (>= 1.0.5)
Enhances: shiny (>= 1.1)
URL: https://github.com/ramnathv/htmlwidgets
BugReports: https://github.com/ramnathv/htmlwidgets/issues
RoxygenNote: 6.0.1
22 changes: 16 additions & 6 deletions R/htmlwidgets.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ createWidget <- function(name,
#' function.
#' @param reportSize Should the widget's container size be reported in the
#' shiny session's client data?
#' @param expr An expression that generates an HTML widget
#' @param expr An expression that generates an HTML widget (or a
#' \href{https://rstudio.github.io/promises/}{promise} of an HTML widget).
#' @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.
Expand Down Expand Up @@ -414,9 +415,7 @@ shinyRenderWidget <- function(expr, outputFunction, env, quoted) {
# generate a function for the expression
func <- shiny::exprToFunction(expr, env, quoted)

# create the render function
renderFunc <- function() {
instance <- func()
renderWidget <- function(instance) {
if (!is.null(instance$elementId)) {
warning("Ignoring explicitly provided widget ID \"",
instance$elementId, "\"; Shiny doesn't use them"
Expand Down Expand Up @@ -448,8 +447,19 @@ shinyRenderWidget <- function(expr, outputFunction, env, quoted) {
toJSON(payload)
}

# mark it with the output function so we can use it in Rmd files
shiny::markRenderFunction(outputFunction, renderFunc)
if (!is.null(asNamespace("shiny")$createRenderFunction)) {
shiny::createRenderFunction(
func,
function(instance, session, name, ...) {
renderWidget(instance)
},
outputFunction, NULL
)
} else {
shiny::markRenderFunction(outputFunction, function() {
renderWidget(func())
})
}
}

checkShinyVersion <- function(error = TRUE) {
Expand Down
7 changes: 7 additions & 0 deletions inst/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ htmlwidgets 1.3 (unreleased)
unintended change in #306. Only the single `WIDGET.js` file should be
copied (where `WIDGET` is the widget name). Fixed via #312.

* Support for async Shiny. Widget render functions that use the default
implementation of `htmlwidgets::shinyRenderWidget` can receive
promises of widget objects (you can, of course, continue to use
regular widget objects as well).

See https://rstudio.github.io/promises for more about async Shiny.

htmlwidgets 1.2
-----------------------------------------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion man/htmlwidgets-shiny.Rd

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

0 comments on commit 6ceb5b5

Please sign in to comment.