Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changes in version 2026.6.5 (PR#336)

- `geom(showSelected.legend=FALSE)` is the canonical opt-out for legend-driven auto-injection of showSelected, including while keeping explicit showSelected variables. (Fixed #333)
- Breaking API cleanup: PR #292 `geom(showSelected=character())` no longer opts out; use `showSelected.legend=FALSE` instead.

# Changes in version 2026.4.28 (PR#292)

- `geom(showSelected=character())` means to opt-out of interactive legends. Thanks @ANAMASGARD.
- `geom(showSelected=character())` opted a layer out of auto-added legend showSelected (superseded by `showSelected.legend=FALSE` in PR#336). Thanks @ANAMASGARD.

# Changes in version 2026.3.8 (PR#311)

Expand Down
2 changes: 1 addition & 1 deletion R/z_animint.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ parsePlot <- function(meta, plot, plot.name){
class(L$mapping) <- "list"

## If any legends are specified, add showSelected aesthetic.
## showSelected=character() opts this layer out of auto-injection only.
## showSelected.legend=FALSE opts this layer out of legend auto-injection only.
L <- addShowSelectedForLegend(meta, plot.info$legend, L)

## Check if showSelected and clickSelects have been used as aesthetics
Expand Down
13 changes: 7 additions & 6 deletions R/z_animintHelpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
#' @param L layer of the plot
#' @return L : Layer with additional mapping to new aesthetic
addShowSelectedForLegend <- function(meta, legend, L){
## Check if user explicitly disabled showSelected with character()
## If showSelected is character(0), user wants to opt out of auto-showSelected
user_disabled_showSelected <- is.character(L$extra_params$showSelected) &&
length(L$extra_params$showSelected) == 0
legend_auto_ss_disabled <- isFALSE(L$extra_params$showSelected.legend)
for(legend.i in seq_along(legend)) {
one.legend <- legend[[legend.i]]
## the name of the selection variable used in this legend.
Expand All @@ -34,7 +31,7 @@ addShowSelectedForLegend <- function(meta, legend, L){
## only add showSelected aesthetic if the variable is
## used by the geom
type.vec <- one.legend$legend_type
if((!user_disabled_showSelected) && any(type.vec %in% names(L$mapping))){
if(!legend_auto_ss_disabled && any(type.vec %in% names(L$mapping))){
L$extra_params$showSelected <- c(L$extra_params$showSelected, s.name)
}
}
Expand Down Expand Up @@ -189,7 +186,10 @@ hjust2anchor <- function(hjust){
error_for_showSelected_variants <- function(params) {
if (is.null(names(params))) return(NULL)
# Match any parameter that starts with "showSelected" but is not exactly "showSelected"
invalid_showSelected <- grep("^showSelected.+", names(params), value = TRUE)
invalid_showSelected <- setdiff(
grep("^showSelected.+", names(params), value = TRUE),
"showSelected.legend"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this is necessary. Can you please revert?

)
if (length(invalid_showSelected) > 0) {
stop(sprintf("Invalid parameter(s): %s. Please use geom(showSelected = character_vector_of_variable_names)",
paste(invalid_showSelected, collapse = ", ")
Expand All @@ -206,6 +206,7 @@ error_for_showSelected_variants <- function(params) {
getLayerParams <- function(l){
params <- c(l$geom_params, l$stat_params, l$aes_params, l$extra_params)
error_for_showSelected_variants(params)
params$showSelected.legend <- NULL
if("chunk_vars" %in% names(params) && is.null(params[["chunk_vars"]])){
params[["chunk_vars"]] <- character()
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-renderer2-interactive-legends.R
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ viz <- list(
) +
geom_segment(
aes(x, y, xend = xend, yend = yend, color = comparison),
showSelected = character(),
showSelected.legend = FALSE,
data = legend.opt.out.segments
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
acontext("opt out of showSelected legend but keep showSelected variable")
## Issue #333 https://github.com/animint/animint2/issues/333
## Issue #333: keep showSelected="year" for filtering, but do not auto-add region from
## colour=region when the legend is shown. API: showSelected.legend=FALSE.
data(WorldBank, package = "animint2")
issue333_viz <- function() {
list(
scatter = ggplot() +
geom_point(
aes(fertility.rate, life.expectancy, colour = region, key = year),
showSelected = "year",
showSelected.legend = FALSE,
data = WorldBank
),
ts = ggplot() + make_tallrect(WorldBank, "year"),
time = list(variable = "year", ms = 3000),
duration = list(year = 3000),
selector.types = list(year = "single"),
title = "issue333 opt out legend keep showSelected"
)
}
showSelected_variables <- function(info, plot.name = "scatter") {
geom.names <- names(info$geoms)
pat <- sprintf("_point_%s$", plot.name)
point.geom.name <- geom.names[grepl(pat, geom.names)]
stopifnot(length(point.geom.name) == 1)
aes.map <- info$geoms[[point.geom.name[[1]]]]$aes
ss.names <- grep("^showSelected", names(aes.map), value = TRUE)
as.character(unlist(aes.map[ss.names]))
}
info <- animint2dir(issue333_viz(), open.browser = FALSE)
test_that("issue333 viz compiles with showSelected.legend=FALSE", {
expect_identical(info$time$variable, "year")
})
test_that("explicit showSelected=year without legend-injected region", {
expect_identical(showSelected_variables(info), "year")
})
test_that("colour legend for region is still compiled", {
legend.info <- info$plots$scatter$legend$region
expect_false(is.null(legend.info))
expect_gt(length(legend.info$entries), 0L)
expect_identical(legend.info$selector, "region")
})
test_that("year selector remains for animation and tallrect", {
expect_identical(info$selectors[["year"]]$type, "single")
})
19 changes: 15 additions & 4 deletions tests/testthat/test-renderer2-legend-without-showSelected.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ test_that("default: legend with color aesthetic creates selector for interactivi
expect_identical(legend_info$selector, "comparison")
expect_identical("comparison" %in% names(info$selectors), TRUE)
})
test_that("showSelected=character() keeps legend selector but opts layer out", {
## Proposed API: empty character vector disables auto showSelected injection
## for this layer, but keeps legend interactivity intact.
test_that("showSelected.legend=FALSE keeps legend selector but opts layer out", {
viz_no_ss <- list(
plot1 = ggplot(test_data, aes(x, y, color = comparison)) +
geom_point(showSelected = character()) +
geom_point(showSelected.legend = FALSE) +
facet_wrap(~facet_var)
)
info <- animint2dir(viz_no_ss, open.browser = FALSE)
Expand All @@ -50,3 +48,16 @@ test_that("showSelected=character() keeps legend selector but opts layer out", {
show_aes_names <- names(info$geoms[[point_geom_name]]$aes)
expect_identical(any(grepl("^showSelected", show_aes_names)), FALSE)
})
test_that("showSelected=character() no longer opts layer out of legend injection", {
viz_char_ss <- list(
plot1 = ggplot(test_data, aes(x, y, color = comparison)) +

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use informative name instead of number (plot1)

geom_point(showSelected = character()) +
facet_wrap(~facet_var)
)
info <- animint2dir(viz_char_ss, open.browser = FALSE)
geom_names <- names(info$geoms)
point_geom_name <- geom_names[grepl("_point_plot1$", geom_names)]
expect_identical(length(point_geom_name), 1L)
show_aes_names <- names(info$geoms[[point_geom_name]]$aes)
expect_identical(any(grepl("^showSelected", show_aes_names)), TRUE)
})
Loading