Skip to content

Commit 3fd7e39

Browse files
authored
Merge pull request #1609 from ropensci/renderWidgetPrep2
Wrapping of user-supplied expressions in renderPlotly
2 parents 08d402c + 1a714b8 commit 3fd7e39

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

R/shiny.R

+21-10
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,20 @@ plotlyOutput <- function(outputId, width = "100%", height = "400px",
3737
#' @rdname plotly-shiny
3838
#' @export
3939
renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
40-
if (!quoted) { expr <- substitute(expr) } # force quoted
41-
# this makes it possible to pass a ggplot2 object to renderPlotly()
42-
# https://github.com/ramnathv/htmlwidgets/issues/166#issuecomment-153000306
43-
expr <- as.call(list(call(":::", quote("plotly"), quote("prepareWidget")), expr))
44-
renderFunc <- shinyRenderWidget(expr, plotlyOutput, env, quoted = TRUE)
40+
if (!quoted) {
41+
quoted <- TRUE
42+
expr <- substitute(expr)
43+
}
44+
# Install the (user-supplied) expression as a function
45+
# This way, if the user-supplied expression contains a return()
46+
# statement, we can capture that return value and pass it along
47+
# to prepareWidget()
48+
# prepareWidget() makes it possible to pass different non-plotly
49+
# objects to renderPlotly() (e.g., ggplot2, promises). It also is used
50+
# to inform event_data about what events have been registered
51+
shiny::installExprFunction(expr, "func", env, quoted, assign.env = env)
52+
expr <- quote(plotly:::prepareWidget(func()))
53+
renderFunc <- shinyRenderWidget(expr, plotlyOutput, env, quoted)
4554
# remove 'internal' plotly attributes that are known to cause false
4655
# positive test results in shinytest (snapshotPreprocessOutput was added
4756
# in shiny 1.0.3.9002, but we require >= 1.1)
@@ -57,13 +66,14 @@ renderPlotly <- function(expr, env = parent.frame(), quoted = FALSE) {
5766

5867
# Converts a plot, OR a promise of a plot, to plotly
5968
prepareWidget <- function(x) {
60-
p <- if (promises::is.promising(x)) {
61-
promises::then(x, ggplotly)
69+
if (promises::is.promising(x)) {
70+
promises::then(
71+
promises::then(x, ggplotly),
72+
register_plot_events
73+
)
6274
} else {
63-
ggplotly(x)
75+
register_plot_events(ggplotly(x))
6476
}
65-
register_plot_events(p)
66-
p
6777
}
6878

6979
register_plot_events <- function(p) {
@@ -73,6 +83,7 @@ register_plot_events <- function(p) {
7383
session$userData$plotlyShinyEventIDs,
7484
eventIDs
7585
))
86+
p
7687
}
7788

7889

0 commit comments

Comments
 (0)