Skip to content

Commit

Permalink
Implement degree_format() (#159) (#178)
Browse files Browse the repository at this point in the history
* Implement degree_format()

* Fix typo in examples

* Add NEWS bullet
  • Loading branch information
mikmart authored and hadley committed Sep 6, 2018
1 parent 508bb2a commit bcf76e6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export(cscale)
export(date_breaks)
export(date_format)
export(date_trans)
export(degree_format)
export(dichromat_pal)
export(discard)
export(div_gradient_pal)
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 1.0.0.9000

* New `degree_format()` adds degree symbols to numbers (@mikmart, #159).

* `number_bytes()` now:

1. Always uses "B" as the symbol for bytes (#174).
Expand Down
26 changes: 26 additions & 0 deletions R/formatter.r
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,32 @@ unit_format <- function(accuracy = 1, scale = 1, prefix = "",
)
}

#' @rdname number_format
#' @export
#' @examples
#' # degree_format()
#' celcius <- degree_format(unit = "C")
#' celcius(rnorm(10) * 10)
#'
#' west <- degree_format(unit = "W")
#' west(runif(10) * 100)
degree_format <- function(accuracy = 1, scale = 1, prefix = "",
unit = "", sep = "",
suffix = paste0(sep, "\u00b0", unit),
big.mark = " ", decimal.mark = ".",
trim = TRUE, ...) {
number_format(
accuracy = accuracy,
scale = scale,
prefix = prefix,
suffix = suffix,
big.mark = big.mark,
decimal.mark = decimal.mark,
trim = trim,
...
)
}

#' @rdname number_format
#' @export
number_si <- function(x, prefix = "", suffix = "", ...) {
Expand Down
11 changes: 11 additions & 0 deletions man/number_format.Rd

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

14 changes: 14 additions & 0 deletions tests/testthat/test-formatter.r
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ test_that("unit format", {
)
})


# Degree formatter --------------------------------------------------------

test_that("Degree format adds degree symbol", {
degree <- degree_format()
expect_equal(degree(c(-10, 0, 20, NA)), c("-10°", "", "20°", "NA°"))
})

test_that("Degree format can also add unit symbols", {
celcius <- degree_format(unit = "C")
expect_equal(celcius(c(-10, 0, 20)), c("-10°C", "0°C", "20°C"))
})


# Percent formatter -------------------------------------------------------

test_that("negative percents work", {
Expand Down

0 comments on commit bcf76e6

Please sign in to comment.