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

Throw error when same argument is passed to aes and geom #119

Merged
merged 7 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 9 additions & 4 deletions R/geom-.r
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,16 @@ Geom <- gganimintproto("Geom",
g$params <- getLayerParams(l)
off_params <- sub("_off", "", grep("_off", names(l$extra_params), value = TRUE))
geom_params <- c(names(l$aes_params), off_params)
duplicate_aes <- intersect(geom_params, names(g$aes))
duplicate_params <- intersect(geom_params, names(g$aes))

if(length(duplicate_aes) > 0) {
stop(paste("Same argument cannot be passed to both aes and geom. Argument passed to both aes and geom:",
paste(duplicate_aes, collapse = ", ")))

if(length(duplicate_params) > 0) {
duplicate_on <- intersect(names(l$aes_params), duplicate_params)
duplicate_off <- setdiff(paste0(intersect(off_params, duplicate_params),"_off"), "_off")
duplicate_geom <- c(duplicate_on, duplicate_off)
stop(paste("Same argument cannot be passed to both aes and geom. Argument passed to aes:",
paste(duplicate_params, collapse = ", "), ". Arguments passed to geom:", paste(duplicate_geom, collapse = ", "),
". The visual property needs only be defined in one place, so if it should be different for each rendered geom, but not depend on selection state, then it should be defined in aes; but if the property should depend on the selection state then it should be defined in geom"))
}

## Make a list of variables to use for subsetting. subset_order is the
Expand Down
21 changes: 18 additions & 3 deletions tests/testthat/test-compiler-errors.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,35 @@ test_that("Same argument passed to aes and geom is an error", {
)
expect_error({
animint2dir(viz, open.browser=FALSE)
}, "Same argument cannot be passed to both aes and geom. Argument passed to both aes and geom: alpha")
}, "Same argument cannot be passed to both aes and geom. Argument passed to aes: alpha . Arguments passed to geom: alpha . The visual property needs only be defined in one place, so if it should be different for each rendered geom, but not depend on selection state, then it should be defined in aes; but if the property should depend on the selection state then it should be defined in geom")
})

test_that("alpha and alpha_off passed to aes and geom is an error", {
scatter <- ggplot()+geom_path(aes(
x=life.expectancy,
y=fertility.rate,
color=region,
group=country, alpha=population),alpha_off=0.5,
group=country, alpha=population),clickSelects="country", alpha_off=0.5,
data=WorldBank)
viz <- list(
plot = scatter
)
expect_error({
animint2dir(viz, open.browser=FALSE)
}, "Same argument cannot be passed to both aes and geom. Argument passed to both aes and geom: alpha")
}, "Same argument cannot be passed to both aes and geom. Argument passed to aes: alpha . Arguments passed to geom: alpha_off . The visual property needs only be defined in one place, so if it should be different for each rendered geom, but not depend on selection state, then it should be defined in aes; but if the property should depend on the selection state then it should be defined in geom")
tdhock marked this conversation as resolved.
Show resolved Hide resolved
})

test_that("alpha_off without clickSelects is a warning", {
scatter <- ggplot()+geom_path(aes(
x=life.expectancy,
y=fertility.rate,
color=region,
group=country),alpha_off=0.5,
data=WorldBank)
viz <- list(
plot = scatter
)
expect_warning({
animint2dir(viz, open.browser=FALSE)
}, "geom1_path_plot has alpha_off which is not used because this geom has no clickSelects; please specify clickSelects or remove alpha_off")
Copy link
Collaborator

Choose a reason for hiding this comment

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

great!

})
Loading