-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New labeller:
label_dictionary()
(#461)
- Loading branch information
Showing
9 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
#' Labels from lookup tables | ||
#' | ||
#' Use `label_dictionary()` for looking up succinct breaks in a named character | ||
#' vector giving complete labels. | ||
#' | ||
#' @param dictionary A named character vector of labels. The names are expected | ||
#' to match the breaks, and the values become the labels. | ||
#' @param nomatch A string to label breaks that do not match any name in | ||
#' `dictionary`. When `NULL` (default), the breaks are not translated but are | ||
#' kept as-is. | ||
#' | ||
#' @return A labeller function that takes a character vector of breaks and | ||
#' returns a character vector of labels. | ||
#' @export | ||
#' @family labels for discrete scales | ||
#' @examples | ||
#' # Example lookup table | ||
#' lut <- c( | ||
#' "4" = "four wheel drive", | ||
#' "r" = "rear wheel drive", | ||
#' "f" = "front wheel drive" | ||
#' ) | ||
#' | ||
#' # Typical usage | ||
#' demo_discrete(c("4", "r", "f"), labels = label_dictionary(lut)) | ||
#' # By default, extra values ('w') will remain as-is | ||
#' demo_discrete(c("4", "r", "f", "w"), labels = label_dictionary(lut)) | ||
#' # Alternatively, you can relabel extra values | ||
#' demo_discrete( | ||
#' c("4", "r", "f", "w"), | ||
#' labels = label_dictionary(lut, nomatch = "unknown") | ||
#' ) | ||
label_dictionary <- function(dictionary = character(), nomatch = NULL) { | ||
|
||
if (!is.character(dictionary)) { | ||
cli::cli_abort("The {.arg dictionary} argument must be a character vector.") | ||
} | ||
if (!is_named2(dictionary)) { | ||
cli::cli_abort("The {.arg dictionary} argument must have names.") | ||
} | ||
names <- names(dictionary) | ||
values <- unname(dictionary) | ||
|
||
force(nomatch) | ||
|
||
function(x) { | ||
i <- match(x, names, nomatch = NA_integer_) | ||
out <- values[i] | ||
missing <- is.na(i) | ||
out[missing] <- if (is.null(nomatch)) x[missing] else nomatch | ||
out | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
test_that("label_dictionary gives correct answers", { | ||
|
||
short <- c("A", "B", "C") | ||
lut <- c("A" = "Apple", "C" = "Cherry", "D" = "Date") | ||
|
||
expect_equal(label_dictionary(lut)(short), c("Apple", "B", "Cherry")) | ||
expect_equal( | ||
label_dictionary(lut, nomatch = "Banana")(short), | ||
c("Apple", "Banana", "Cherry") | ||
) | ||
}) |