Skip to content

Commit

Permalink
Updated default dp for pretty_num (#93)
Browse files Browse the repository at this point in the history
* updated the dp default to be 0

reverted tests for pretty_num to og ones and added more tests in pretty_num

amended tests in pretty_num table

updated documentation

* applying changes to code style to pass lint check

* updated documentation

* updated formatting of documentation for spelling

* more updates to the documentation before updating package versions

* updated documentation and readme
  • Loading branch information
mzayeddfe authored Oct 17, 2024
1 parent 23dd8da commit f5bcf08
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: dfeR
Title: Common DfE R tasks
Version: 0.6.0
Version: 0.6.1
Authors@R: c(
person("Cam", "Race", , "[email protected]", role = c("aut", "cre")),
person("Laura", "Selby", , "[email protected]", role = "aut"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# dfeR 0.6.1

Patch to update the pretty_num() function so that the `dp` argument's default is 0.

# dfeR 0.6.0

Update pretty_num so that:
Expand Down
11 changes: 7 additions & 4 deletions R/pretty.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pretty_time_taken <- function(start_time, end_time) {
#' Uses `as.numeric()` to force a numeric value and then formats prettily
#' for easy presentation in console messages, reports, or dashboards.
#'
#' This rounds to 2 decimal places by default, and adds in comma separators.
#' This rounds to 0 decimal places by default, and adds in comma separators.
#'
#' Expect that this will commonly be used for adding the pound symbol,
#' the percentage symbol, or to have a +/- prefixed based on the value.
Expand All @@ -184,11 +184,14 @@ pretty_time_taken <- function(start_time, end_time) {
#' assign + or - based on the value
#' @param gbp whether to add the pound symbol or not, defaults to not
#' @param suffix suffix for the value, e.g. "%"
#' @param dp number of decimal places to round to, 2 by default
#' @param dp number of decimal places to round to, 0 by default.
#' @param ignore_na whether to skip function for strings that can't be
#' converted and return original value
#' @param alt_na alternative value to return in place of NA, e.g. "x"
#' @param nsmall minimum number of digits to the right of the decimal point
#' @param nsmall minimum number of digits to the right of the decimal point.
#' If NULL, the value of `dp` will be used.
#' If the value of `dp` is less than 0, then `nsmall` will
#' automatically be set to 0.
#'
#' @return string featuring prettified value
#' @family prettying
Expand Down Expand Up @@ -223,7 +226,7 @@ pretty_num <- function(
prefix = "",
gbp = FALSE,
suffix = "",
dp = 2,
dp = 0,
ignore_na = FALSE,
alt_na = FALSE,
nsmall = NULL) {
Expand Down
10 changes: 6 additions & 4 deletions man/pretty_num.Rd

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

27 changes: 17 additions & 10 deletions tests/testthat/test-pretty_num.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
test_that("prettifies", {
expect_equal(pretty_num(1, gbp = TRUE, suffix = " offer"), "£1.00 offer")
expect_equal(pretty_num(-1), "-1.00")
expect_equal(pretty_num(-1, prefix = "-"), "--1.00")
expect_equal(pretty_num(-1, prefix = "+/-"), "-1.00")
expect_equal(pretty_num(1, prefix = "+/-"), "+1.00")
expect_equal(pretty_num(12.289009, suffix = "%"), "12.29%")
expect_equal(pretty_num(1000), "1,000.00")
expect_equal(pretty_num(1, gbp = TRUE, suffix = " offer"), "£1 offer")
expect_equal(pretty_num(-1, dp = 2), "-1.00")
expect_equal(pretty_num(-1), "-1")
expect_equal(pretty_num(-1, prefix = "-"), "--1")
expect_equal(pretty_num(-1, prefix = "+/-"), "-1")
expect_equal(pretty_num(-1, dp = 2, prefix = "+/-"), "-1.00")
expect_equal(pretty_num(1, prefix = "+/-"), "+1")
expect_equal(pretty_num(12.289009, dp = 2, suffix = "%"), "12.29%")
expect_equal(pretty_num(1000), "1,000")
expect_equal(pretty_num(11^8, gbp = TRUE, dp = -1), "£210 million")
expect_equal(pretty_num(11^9, gbp = TRUE, dp = 3), "£2.358 billion")
expect_equal(pretty_num(-11^8, gbp = TRUE, dp = -1), "-£210 million")
expect_equal(pretty_num(-123421421), "-123.42 million")
expect_equal(pretty_num(-123421421, dp = 2), "-123.42 million")
expect_equal(pretty_num(63.71, dp = 1, nsmall = 2), "63.70")
expect_equal(pretty_num(894.1, dp = 2, nsmall = 3), "894.100")
expect_equal(
pretty_num(11^8, prefix = "+/-", gbp = TRUE, dp = -1.00), "+£210 million"
pretty_num(11^8, prefix = "+/-", gbp = TRUE, dp = -1), "+£210 million"
)
})

Expand All @@ -27,11 +29,16 @@ test_that("handles NAs", {
test_that("tests multiple values", {
expect_equal(
pretty_num(c(1:4)),
c("1.00", "2.00", "3.00", "4.00")
c("1", "2", "3", "4")
)

expect_equal(
pretty_num(c(1:4), nsmall = 1),
c("1.0", "2.0", "3.0", "4.0")
)

expect_equal(
pretty_num(c(1.478, 7.38897, 8.37892), dp = 1, nsmall = 2),
c("1.50", "7.40", "8.40")
)
})
19 changes: 17 additions & 2 deletions tests/testthat/test-pretty_num_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,36 @@ df <- data.frame(


test_that("prettifies tables", {
expect_equal(pretty_num_table(df), data.frame(
expect_equal(pretty_num_table(df, dp = 2), data.frame(
a = c("2.59", "-5.89", as.double(NA)),
b = c("11.20", "45.69", "-78.50"),
c = c(as.double(NA), as.double(NA), as.double(NA))
))

expect_equal(pretty_num_table(df, dp = 3), data.frame(
a = c("2.589", "-5.894", as.double(NA)),
b = c("11.199", "45.689", "-78.499"),
c = c(as.double(NA), as.double(NA), as.double(NA))
))

expect_equal(
pretty_num_table(df, gbp = TRUE, exclude_columns = "c"),
pretty_num_table(df, dp = 2, gbp = TRUE, exclude_columns = "c"),
data.frame(
a = c("£2.59", "-£5.89", as.double(NA)),
b = c("£11.20", "£45.69", "-£78.50"),
c = c("X", "Y", "Z")
)
)

expect_equal(
pretty_num_table(df, gbp = TRUE, exclude_columns = "c"),
data.frame(
a = c("£3", "-£6", as.double(NA)),
b = c("£11", "£46", "-£78"),
c = c("X", "Y", "Z")
)
)


expect_equal(
pretty_num_table(df,
Expand Down

0 comments on commit f5bcf08

Please sign in to comment.