Refactor palette checks to use checkmate assertions
This commit is contained in:
parent
94045e30c0
commit
bf76ad06a7
2 changed files with 20 additions and 13 deletions
|
|
@ -16,13 +16,13 @@ palette <- function(
|
|||
) {
|
||||
#------ Checks
|
||||
|
||||
# Check that palette is a character scalar
|
||||
# palette is a character scalar
|
||||
checkmate::assert_character(palette, len = 1)
|
||||
|
||||
# Check that reverse is a logical scalar
|
||||
# reverse is a logical scalar
|
||||
checkmate::assert_logical(reverse, len = 1)
|
||||
|
||||
# Check that show_palettes is a logical scalar
|
||||
# show_palettes is a logical scalar
|
||||
checkmate::assert_logical(show_palettes, len = 1)
|
||||
|
||||
#------ Get colors
|
||||
|
|
|
|||
|
|
@ -9,9 +9,12 @@
|
|||
#'
|
||||
#' @export
|
||||
palette_gen <- function(palette, type, direction = 1, ...) {
|
||||
if (type %notin% c("categorical", "sequential", "divergent")) {
|
||||
rlang::abort("'type' must be categorical or continuous or divergent.")
|
||||
}
|
||||
#------ Checks
|
||||
|
||||
checkmate::assert_string(palette)
|
||||
checkmate::assert_choice(type, c("categorical", "sequential", "divergent"))
|
||||
checkmate::assert_number(direction, lower = -1, upper = 1)
|
||||
checkmate::assert_true(abs(direction) == 1)
|
||||
|
||||
if (type == "categorical") {
|
||||
return(palette_gen_categorical(palette = palette, direction = direction))
|
||||
|
|
@ -31,9 +34,11 @@ palette_gen <- function(palette, type, direction = 1, ...) {
|
|||
#'
|
||||
#' @export
|
||||
palette_gen_categorical <- function(palette = "cat_5_main", direction = 1) {
|
||||
if (abs(direction) != 1) {
|
||||
rlang::abort("Direction must be either 1 or -1.")
|
||||
}
|
||||
#------ Checks
|
||||
|
||||
checkmate::assert_string(palette)
|
||||
checkmate::assert_number(direction, lower = -1, upper = 1)
|
||||
checkmate::assert_true(abs(direction) == 1)
|
||||
|
||||
pal <- palette(palette)
|
||||
|
||||
|
|
@ -59,10 +64,12 @@ palette_gen_categorical <- function(palette = "cat_5_main", direction = 1) {
|
|||
#' @rdname palette_gen
|
||||
#'
|
||||
#' @export
|
||||
palette_gen_sequential <- function(palette = "seq_5_main", direction = 1, ...) {
|
||||
if (abs(direction) != 1) {
|
||||
rlang::abort("Direction must be either 1 or -1.")
|
||||
}
|
||||
palette_gen_sequential <- function(palette = "cat_5_main", direction = 1, ...) {
|
||||
#------ Checks
|
||||
|
||||
checkmate::assert_string(palette)
|
||||
checkmate::assert_number(direction, lower = -1, upper = 1)
|
||||
checkmate::assert_true(abs(direction) == 1)
|
||||
|
||||
pal <- palette(palette)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue