Skip to content

Commit

Permalink
document
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Oct 7, 2024
1 parent e1d0e49 commit 1679a57
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
LazyLoad: yes
Roxygen: list(markdown = TRUE, r6 = FALSE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export(label_date)
export(label_date_short)
export(label_dollar)
export(label_log)
export(label_lut)
export(label_math)
export(label_number)
export(label_number_auto)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# scales (development version)

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

# scales 1.3.0

## Better type support
Expand Down
31 changes: 31 additions & 0 deletions R/label-lut.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@

#' Labels from lookup tables
#'
#' Use `label_lut()` 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.
#'
#' @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_lut(lut))
#' # By default, extra values ('w') will remain as-is
#' demo_discrete(c("4", "r", "f", "w"), labels = label_lut(lut))
#' # Alternatively, you can relabel extra values
#' demo_discrete(
#' c("4", "r", "f", "w"),
#' labels = label_lut(lut, nomatch = "unknown")
#' )
label_lut <- function(lut = character(), nomatch = NULL) {

if (!is.character(lut)) {
Expand Down
47 changes: 47 additions & 0 deletions man/label_lut.Rd

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

1 change: 1 addition & 0 deletions man/label_parse.Rd

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

1 change: 1 addition & 0 deletions man/label_wrap.Rd

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

0 comments on commit 1679a57

Please sign in to comment.