Skip to content

Commit cbb60e6

Browse files
committed
Add option to suppress spurious warnings in event_data()
1 parent cc49ee5 commit cbb60e6

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

R/shiny.R

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ register_plot_events <- function(p) {
113113
#' If equal to `"event"`, then [event_data()] always triggers re-execution,
114114
#' instead of re-executing only when the relevant shiny input value changes
115115
#' (the default).
116+
#' @param suppress_unregistered_warning If TRUE, do not throw a warning when
117+
#' this function is called before a plot renders and registers event handlers.
118+
#' (These warnings often occur spuriously in multi-tab apps, where not all plots
119+
#' are immediately rendered.)
116120
#' @export
117121
#' @seealso [event_register], [event_unregister]
118122
#' @references
@@ -133,7 +137,8 @@ event_data <- function(
133137
),
134138
source = "A",
135139
session = shiny::getDefaultReactiveDomain(),
136-
priority = c("input", "event")
140+
priority = c("input", "event"),
141+
suppress_unregistered_warning = FALSE
137142
) {
138143
if (is.null(session)) {
139144
stop("No reactive domain detected. This function can only be called \n",
@@ -143,25 +148,30 @@ event_data <- function(
143148
event <- match.arg(event)
144149
eventID <- paste(event, source, sep = "-")
145150

146-
# It's possible for event_data() to execute before any
147-
# relevant input values have been registered (i.e, before
148-
# relevant plotly graphs have been executed). Therefore,
149-
# we delay checking that a relevant input value has been
150-
# registered until shiny flushes
151-
session$onFlushed(
152-
function() {
153-
eventIDRegistered <- eventID %in% session$userData$plotlyShinyEventIDs
154-
if (!eventIDRegistered) {
155-
warning(
156-
"The '", event, "' event tied a source ID of '", source, "' ",
157-
"is not registered. In order to obtain this event data, ",
158-
"please add `event_register(p, '", event, "')` to the plot (`p`) ",
159-
"that you wish to obtain event data from.",
160-
call. = FALSE
161-
)
151+
if (!suppress_unregistered_warning) {
152+
# It's possible for event_data() to execute before any
153+
# relevant input values have been registered (i.e, before
154+
# relevant plotly graphs have been executed). Therefore,
155+
# we delay checking that a relevant input value has been
156+
# registered until shiny flushes
157+
session$onFlushed(
158+
function() {
159+
eventIDRegistered <- eventID %in% session$userData$plotlyShinyEventIDs
160+
if (!eventIDRegistered) {
161+
warning(
162+
"The '", event, "' event tied a source ID of '", source, "' ",
163+
"is not registered. In order to obtain this event data, ",
164+
"please add `event_register(p, '", event, "')` to the plot (`p`) ",
165+
"that you wish to obtain event data from, or set ",
166+
"`suppress_unregistered_warning = TRUE` to suppress this warning ",
167+
"if the plot will eventually register this event, but only ",
168+
"renders conditionally.",
169+
call. = FALSE
170+
)
171+
}
162172
}
163-
}
164-
)
173+
)
174+
}
165175

166176
# legend clicking returns trace(s), which shouldn't be simplified...
167177
parseJSON <- if (event %in% c("plotly_legendclick", "plotly_legenddoubleclick")) {

0 commit comments

Comments
 (0)