Skip to content
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

Add suppress_unregistered_warning parameter to event_data() to prevent spurious warnings in apps with conditionally-rendered plots #2410

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Expand Up @@ -78,7 +78,7 @@ Suggests:
rsvg,
ggridges
LazyData: true
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Config/Needs/check:
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* `ggplotly()` now supports the `{ggridges}` package. (#2314)

* `event_data()` has a new `suppress_unregistered_warning` parameter. The relevant warning message has also been updated to reflect that it is often spurious in apps with conditionally-rendered plots (e.g. dashboard apps with multiple tabs). (#1528, #1538)

## Improvements

* `ggplotly()` now works better with the development version of ggplot2 (> v3.4.4). (#2315, #2368)
Expand Down
48 changes: 29 additions & 19 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ register_plot_events <- function(p) {
#' If equal to `"event"`, then [event_data()] always triggers re-execution,
#' instead of re-executing only when the relevant shiny input value changes
#' (the default).
#' @param suppress_unregistered_warning If TRUE, do not throw a warning when
#' this function is called before a plot renders and registers event handlers.
#' (These warnings often occur spuriously in multi-tab apps, where not all plots
#' are immediately rendered.)
#' @export
#' @seealso [event_register], [event_unregister]
#' @references
Expand All @@ -133,7 +137,8 @@ event_data <- function(
),
source = "A",
session = shiny::getDefaultReactiveDomain(),
priority = c("input", "event")
priority = c("input", "event"),
suppress_unregistered_warning = FALSE
) {
if (is.null(session)) {
stop("No reactive domain detected. This function can only be called \n",
Expand All @@ -143,25 +148,30 @@ event_data <- function(
event <- match.arg(event)
eventID <- paste(event, source, sep = "-")

# It's possible for event_data() to execute before any
# relevant input values have been registered (i.e, before
# relevant plotly graphs have been executed). Therefore,
# we delay checking that a relevant input value has been
# registered until shiny flushes
session$onFlushed(
function() {
eventIDRegistered <- eventID %in% session$userData$plotlyShinyEventIDs
if (!eventIDRegistered) {
warning(
"The '", event, "' event tied a source ID of '", source, "' ",
"is not registered. In order to obtain this event data, ",
"please add `event_register(p, '", event, "')` to the plot (`p`) ",
"that you wish to obtain event data from.",
call. = FALSE
)
if (!suppress_unregistered_warning) {
# It's possible for event_data() to execute before any
# relevant input values have been registered (i.e, before
# relevant plotly graphs have been executed). Therefore,
# we delay checking that a relevant input value has been
# registered until shiny flushes
session$onFlushed(
function() {
eventIDRegistered <- eventID %in% session$userData$plotlyShinyEventIDs
if (!eventIDRegistered) {
warning(
"The '", event, "' event tied a source ID of '", source, "' ",
"is not registered. In order to obtain this event data, ",
"please add `event_register(p, '", event, "')` to the plot (`p`) ",
"that you wish to obtain event data from, or set ",
"`suppress_unregistered_warning = TRUE` to suppress this warning ",
"if the plot will eventually register this event, but only ",
"renders conditionally.",
call. = FALSE
)
}
}
}
)
)
}

# legend clicking returns trace(s), which shouldn't be simplified...
parseJSON <- if (event %in% c("plotly_legendclick", "plotly_legenddoubleclick")) {
Expand Down
8 changes: 7 additions & 1 deletion man/event_data.Rd

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

Loading