Skip to content

Commit

Permalink
fix: add judgment on named vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
liang09255 committed Jul 25, 2023
1 parent 23237d3 commit a1ef2ae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion R/scale-manual.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ...,
if (n > length(values)) {
cli::cli_abort("Insufficient values in manual scale. {n} needed but only {length(values)} provided.")
}
unname(values[seq_len(n)])

if (is.null(names(values))) {
values[seq_len(n)]
} else {
values
}
}
discrete_scale(aesthetic, "manual", pal, breaks = breaks, limits = limits, ...)
}
2 changes: 1 addition & 1 deletion tests/testthat/test-scale-manual.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_that("names of values used in manual scales", {

test_that("obtain expected quantity values from manual scale", {
for (aesthetics in .all_aesthetics) {
s <- scale_discrete_manual(aesthetics = aesthetics, values = c("8" = "c", "4" = "a", "6" = "b"))
s <- scale_discrete_manual(aesthetics = aesthetics, values = c("c", "a", "b"))
expect_equal(s$palette(1), c("c"))
expect_equal(s$palette(2), c("c", "a"))
expect_equal(s$palette(3), c("c", "a", "b"))
Expand Down

0 comments on commit a1ef2ae

Please sign in to comment.