Skip to content
Merged
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: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Fix a bug about handling Geoms that have some non-primitive value as a default
for an aesthetic (#195).

* Treat `from_theme(foo %||% NA)` as `NA` (#216).

* `gghighlight_point()` and `gghighlight_line()` are removed.

# gghighlight 0.4.1
Expand Down
6 changes: 4 additions & 2 deletions R/gghighlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,10 @@ get_default_aes_param <- function(
if (
aes_param_name %in%
names(non_null_default_aes) &&
# is.na() cannot handle non primitive objects (e.g. a quosure)
rlang::is_na(non_null_default_aes[[aes_param_name]])
# check if
# - x is NA
# - x is quo(from_theme(foo %|% NA))
is_na_aes(non_null_default_aes[[aes_param_name]])
) {
return(NA)
}
Expand Down
40 changes: 40 additions & 0 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,43 @@ make_label <- function(x) {
}
name
}

# return TRUE if
# - x is NA
# - x is quo(from_theme(foo %|% NA))
is_na_aes <- function(x) {
if (is_na(x)) {
return(TRUE)
}

# if X is not NA, inspect inside the quosure

if (!is_quosure(x)) {
return(FALSE)
}

x <- quo_squash(x)
if (is_na(x)) {
return(TRUE)
}

if (!is_call(x) || call_name(x) != "from_theme") {
return(FALSE)
}

args <- call_args(x)

if (
length(args) == 1L && !is_call(args[[1]]) || call_name(args[[1]]) != "%||%"
) {
return(FALSE)
}

args_inner <- call_args(args[[1]])

if (length(args_inner) != 2L) {
return(FALSE)
}

is_na(args_inner[[2]])
}
152 changes: 76 additions & 76 deletions tests/testthat/_snaps/vdiffr/simple-bar-chart-with-facet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 19 additions & 19 deletions tests/testthat/_snaps/vdiffr/simple-bar-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion tests/testthat/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ expect_equal_layer <- function(x, y) {
x$constructor <- NULL
y$constructor <- NULL

x$computed_mapping <- NULL
y$computed_mapping <- NULL

x$aes_params <- x$aes_params[sort(names(x$aes_params))]
y$aes_params <- y$aes_params[sort(names(y$aes_params))]
x$mapping <- x$mapping[sort(names(x$mapping))]
Expand All @@ -17,10 +20,19 @@ expect_equal_layer <- function(x, y) {
}

as_no_label_list <- function(x) {
lapply(x, \(x) {
x <- lapply(x, \(x) {
attr(x, "label") <- NULL
x
})

if (!is.null(x$data)) {
x$data <- lapply(x$data, \(x) {
attr(x, "label") <- NULL
x
})
}

x
}

expect_equal_layers <- function(x, y) {
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-bleach.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ test_that("bleach_layer() works", {
geom_line(aes(colour = NULL, fill = NULL), d, colour = grey07)
)

# If the geom accepts fill, it is sets to grey even when it is not included in the mapping.
# If the geom accepts fill, it is set to grey even when it is not included in the mapping.
expect_equal_layer(
bleach_layer(geom_col(aes(colour = type), d), g_info, list()),
geom_col(aes_bleached, d_bleached, colour = grey07, fill = grey07)
geom_col(aes_bleached, d_bleached, colour = NA, fill = grey07)
Copy link
Owner Author

Choose a reason for hiding this comment

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

Honestly, I'm not sure why I wrote the expectation as non-NA, so I'm no longer sure what value this is supposed to be...

)

# If the default of colour of the geom is NA and mapping doesn't specify it, params will be NA.
Expand All @@ -62,7 +62,7 @@ test_that("bleach_layer() works", {
# If colour and fill is specified at the same time, fill is used as the group key.
expect_equal_layer(
bleach_layer(geom_col(aes(colour = type, fill = type), d), g_info, list()),
geom_col(aes_bleached, d_bleached, colour = grey07, fill = grey07)
geom_col(aes_bleached, d_bleached, colour = NA, fill = grey07)
)

# If mapping doesn't have colour or fill, group or x aes can be used as group key.
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-gghighlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ test_that("gghighlight() works with two layers, grouped", {
d_bleached2,
shape = "circle filled",
colour = grey07,
fill = grey07
fill = NA
)
l_sieved_2 <- geom_point(
aes(x, y, colour = type, fill = type),
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,14 @@ test_that("get_facet_vars() extract facet specs", {
class(p$facet) <- c("FacetUnknown", class(p$facet))
expect_error(get_facet_vars(p$facet))
})

test_that("is_na_aes() works", {
expect_true(is_na_aes(NA))
expect_true(is_na_aes(quo(NA)))
expect_true(is_na_aes(quo(from_theme(foo %||% NA))))
expect_true(is_na_aes(quo(from_theme(foo %||% NA_character_))))

expect_false(is_na_aes(quo(foo)))
expect_false(is_na_aes(quo(from_theme(foo))))
expect_false(is_na_aes(quo(from_theme(foo %||% 1))))
})