Skip to content

Commit

Permalink
rename to label_dictionary()
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Oct 22, 2024
1 parent 94bfd63 commit 29751e8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ export(label_comma)
export(label_currency)
export(label_date)
export(label_date_short)
export(label_dictionary)
export(label_dollar)
export(label_glue)
export(label_log)
export(label_lut)
export(label_math)
export(label_number)
export(label_number_auto)
Expand Down
3 changes: 1 addition & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
using `get_palette()` or registered using `set_palette()` (#396).
* `label_log()` has a `signed` argument for displaying negative numbers
(@teunbrand, #421).

* New `label_lut()` for named lookup of labels (#458).
* New `label_dictionary()` for named lookup of labels (#458).

# scales 1.3.0

Expand Down
21 changes: 11 additions & 10 deletions R/label-lut.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

#' Labels from lookup tables
#'
#' Use `label_lut()` for looking up succinct breaks in a named character vector
#' giving complete labels.
#' Use `label_dictionary()` for looking up succinct breaks in a named character
#' vector giving complete labels.
#'
#' @param lut 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 `lut`.
#' When `NULL` (default), the breaks are not translated but are kept as-is.
#' @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.
Expand All @@ -22,15 +23,15 @@
#' )
#'
#' # Typical usage
#' demo_discrete(c("4", "r", "f"), labels = label_lut(lut))
#' 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_lut(lut))
#' 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_lut(lut, nomatch = "unknown")
#' labels = label_dictionary(lut, nomatch = "unknown")
#' )
label_lut <- function(lut = character(), nomatch = NULL) {
label_dictionary <- function(dictionary = character(), nomatch = NULL) {

if (!is.character(lut)) {
cli::cli_abort("The {.arg lut} argument must be a character vector.")
Expand Down
25 changes: 13 additions & 12 deletions man/label_lut.Rd → man/label_dictionary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/label_glue.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/label_parse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/label_wrap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/testthat/test-label-lut.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
test_that("label_lut gives correct answers", {
test_that("label_dictionary gives correct answers", {

short <- c("A", "B", "C")
lut <- c("A" = "Apple", "C" = "Cherry", "D" = "Date")

expect_equal(label_lut(lut)(short), c("Apple", "B", "Cherry"))
expect_equal(label__dictionary(lut)(short), c("Apple", "B", "Cherry"))
expect_equal(
label_lut(lut, nomatch = "Banana")(short),
label_dictionary(lut, nomatch = "Banana")(short),
c("Apple", "Banana", "Cherry")
)
})

0 comments on commit 29751e8

Please sign in to comment.