Skip to content

Commit

Permalink
Merge branch 'master' into data-arg
Browse files Browse the repository at this point in the history
* master:
  Add the `force_sign` arg to several numeric formatters (#793)
  • Loading branch information
rich-iannone committed Jun 10, 2021
2 parents b1b3713 + 3c70565 commit 9c788fd
Show file tree
Hide file tree
Showing 19 changed files with 657 additions and 58 deletions.
238 changes: 180 additions & 58 deletions R/format_data.R

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions man/fmt_bytes.Rd

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

7 changes: 7 additions & 0 deletions man/fmt_currency.Rd

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

6 changes: 6 additions & 0 deletions man/fmt_engineering.Rd

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

7 changes: 7 additions & 0 deletions man/fmt_integer.Rd

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

7 changes: 7 additions & 0 deletions man/fmt_number.Rd

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

7 changes: 7 additions & 0 deletions man/fmt_percent.Rd

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

6 changes: 6 additions & 0 deletions man/fmt_scientific.Rd

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

31 changes: 31 additions & 0 deletions tests/testthat/test-fmt_bytes.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,37 @@ test_that("the `fmt_bytes()` function works correctly", {
)
)

# Format the `num` column to 3 decimal places, force the sign
expect_equal(
(tab %>%
fmt_bytes(
columns = num, decimals = 3, force_sign = TRUE) %>%
render_formats_test("html"))[["num"]],
c(
"−500 B", "0 B", "0 B", "0 B", "+500 B", "+1.023 kB",
"+1.001 kB", "+1.024 kB", "+1.049 MB", "+1.074 GB", "+1.1 TB",
"+1.126 PB", "+1.153 EB", "+1.181 ZB", "+1.209 YB", "+1 kB",
"+1 MB", "+1 GB", "+1 TB", "+1 PB", "+1 EB", "+1 ZB", "+1 YB",
"+15 YB", "+150 YB", "+1,500 YB", "+15,000 YB", "NA"
)
)

# Format the `num` column, force the sign and
# define a pattern for decorating values
expect_equal(
(tab %>%
fmt_bytes(
columns = num, pattern = "*{x}*", force_sign = TRUE) %>%
render_formats_test("html"))[["num"]],
c(
"*−500 B*", "*0 B*", "*0 B*", "*0 B*", "*+500 B*", "*+1 kB*",
"*+1 kB*", "*+1 kB*", "*+1 MB*", "*+1.1 GB*", "*+1.1 TB*", "*+1.1 PB*",
"*+1.2 EB*", "*+1.2 ZB*", "*+1.2 YB*", "*+1 kB*", "*+1 MB*",
"*+1 GB*", "*+1 TB*", "*+1 PB*", "*+1 EB*", "*+1 ZB*", "*+1 YB*",
"*+15 YB*", "*+150 YB*", "*+1,500 YB*", "*+15,000 YB*", "NA"
)
)

# Format the `num` column to 2 decimal places, apply the `en_US`
# locale and use all other defaults; extract `output_df` and compare
# to expected values
Expand Down
65 changes: 65 additions & 0 deletions tests/testthat/test-fmt_engineering.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,71 @@ test_that("the `fmt_engineering()` function works correctly", {
)
)

# Format the `num` column to 2 decimal places, force the sign
expect_equal(
(tab %>%
fmt_engineering(
columns = "num", decimals = 3, force_sign = TRUE) %>%
render_formats_test("html"))[["num"]],
c(
"+82.030 &times; 10<sup class='gt_super'>30</sup>",
"+829.300 &times; 10<sup class='gt_super'>18</sup>",
"+492.032 &times; 10<sup class='gt_super'>9</sup>",
"+84.930 &times; 10<sup class='gt_super'>9</sup>",
"+5.043 &times; 10<sup class='gt_super'>9</sup>",
"+203.821 &times; 10<sup class='gt_super'>6</sup>",
"+84.729 &times; 10<sup class='gt_super'>6</sup>",
"+2.323 &times; 10<sup class='gt_super'>6</sup>",
"+230.323 &times; 10<sup class='gt_super'>3</sup>",
"+50.000 &times; 10<sup class='gt_super'>3</sup>",
"+1.000 &times; 10<sup class='gt_super'>3</sup>", "+10.000",
"+12.345 &times; 10<sup class='gt_super'>3</sup>",
"+1.234 &times; 10<sup class='gt_super'>3</sup>", "+123.450", "+1.234",
"+123.450 &times; 10<sup class='gt_super'>&minus;3</sup>",
"+12.346 &times; 10<sup class='gt_super'>&minus;6</sup>",
"&minus;50.000 &times; 10<sup class='gt_super'>3</sup>",
"&minus;1.000 &times; 10<sup class='gt_super'>3</sup>", "&minus;10.000",
"&minus;12.345 &times; 10<sup class='gt_super'>3</sup>",
"&minus;1.234 &times; 10<sup class='gt_super'>3</sup>",
"&minus;123.450", "&minus;1.234",
"&minus;123.450 &times; 10<sup class='gt_super'>&minus;3</sup>",
"&minus;12.346 &times; 10<sup class='gt_super'>&minus;6</sup>"
)
)

# Format the `num` column to 2 decimal places, force the sign and
# define a pattern for decorating values
expect_equal(
(tab %>%
fmt_engineering(
columns = "num", pattern = "*{x}*", force_sign = TRUE) %>%
render_formats_test("html"))[["num"]],
c(
"*+82.03 &times; 10<sup class='gt_super'>30</sup>*",
"*+829.30 &times; 10<sup class='gt_super'>18</sup>*",
"*+492.03 &times; 10<sup class='gt_super'>9</sup>*",
"*+84.93 &times; 10<sup class='gt_super'>9</sup>*",
"*+5.04 &times; 10<sup class='gt_super'>9</sup>*",
"*+203.82 &times; 10<sup class='gt_super'>6</sup>*",
"*+84.73 &times; 10<sup class='gt_super'>6</sup>*",
"*+2.32 &times; 10<sup class='gt_super'>6</sup>*",
"*+230.32 &times; 10<sup class='gt_super'>3</sup>*",
"*+50.00 &times; 10<sup class='gt_super'>3</sup>*",
"*+1.00 &times; 10<sup class='gt_super'>3</sup>*", "*+10.00*",
"*+12.35 &times; 10<sup class='gt_super'>3</sup>*",
"*+1.23 &times; 10<sup class='gt_super'>3</sup>*", "*+123.45*", "*+1.23*",
"*+123.45 &times; 10<sup class='gt_super'>&minus;3</sup>*",
"*+12.35 &times; 10<sup class='gt_super'>&minus;6</sup>*",
"*&minus;50.00 &times; 10<sup class='gt_super'>3</sup>*",
"*&minus;1.00 &times; 10<sup class='gt_super'>3</sup>*", "*&minus;10.00*",
"*&minus;12.35 &times; 10<sup class='gt_super'>3</sup>*",
"*&minus;1.23 &times; 10<sup class='gt_super'>3</sup>*",
"*&minus;123.45*", "*&minus;1.23*",
"*&minus;123.45 &times; 10<sup class='gt_super'>&minus;3</sup>*",
"*&minus;12.35 &times; 10<sup class='gt_super'>&minus;6</sup>*"
)
)

# Format the `num` column to 2 decimal places, apply the `en_US`
# locale and use all other defaults
expect_equal(
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-fmt_integer.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,38 @@ test_that("the `fmt_integer()` function works correctly in the HTML context", {
c("a1,836b", "a2,763b", "a937b", "a643b", "a212b", "a0b", "a(23)b")
)

# Format the `num_1` column to 2 decimal places, force the sign
expect_equal(
(tab %>%
fmt_integer(
columns = num_1, force_sign = TRUE) %>%
render_formats_test("html"))[["num_1"]],
c("+1,836", "+2,763", "+937", "+643", "+212", "0", "&minus;23")
)

# Expect that using `force_sign = TRUE` with `accounting = TRUE`
# will render values in accounting format
expect_equal(
(tab %>%
fmt_integer(
columns = num_1, accounting = TRUE, force_sign = TRUE) %>%
render_formats_test("html"))[["num_1"]],
(tab %>%
fmt_integer(
columns = num_1, accounting = TRUE) %>%
render_formats_test("html"))[["num_1"]]
)

# Format the `num_1` column to 2 decimal places, force the sign and
# define a pattern for decorating values
expect_equal(
(tab %>%
fmt_integer(
columns = num_1, pattern = "*{x}*", force_sign = TRUE) %>%
render_formats_test("html"))[["num_1"]],
c("*+1,836*", "*+2,763*", "*+937*", "*+643*", "*+212*", "*0*", "*&minus;23*")
)

# Format the `num_1`, apply the `en_US`
# locale and use all other defaults
expect_equal(
Expand Down
35 changes: 35 additions & 0 deletions tests/testthat/test-fmt_number.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,41 @@ test_that("the `fmt_number()` function works correctly in the HTML context", {
c("1,836.23", "2,763.39", "937.29", "643", "212.232", "0", "(23.24)")
)

# Format the `num_1` column to 2 decimal places, force the sign
expect_equal(
(tab %>%
fmt_number(
columns = num_1, decimals = 3, force_sign = TRUE) %>%
render_formats_test("html"))[["num_1"]],
c("+1,836.230", "+2,763.390", "+937.290", "+643.000", "+212.232", "0.000", "&minus;23.240")
)

# Expect that using `force_sign = TRUE` with `accounting = TRUE`
# will render values in accounting format
expect_equal(
(tab %>%
fmt_number(
columns = num_1, decimals = 3, accounting = TRUE, force_sign = TRUE) %>%
render_formats_test("html"))[["num_1"]],
(tab %>%
fmt_number(
columns = num_1, decimals = 3, accounting = TRUE) %>%
render_formats_test("html"))[["num_1"]]
)

# Format the `num_1` column to 2 decimal places, force the sign and
# define a pattern for decorating values
expect_equal(
(tab %>%
fmt_number(
columns = num_1, pattern = "*{x}*", force_sign = TRUE) %>%
render_formats_test("html"))[["num_1"]],
c(
"*+1,836.23*", "*+2,763.39*", "*+937.29*", "*+643.00*", "*+212.23*",
"*0.00*", "*&minus;23.24*"
)
)

# Format the `num_1` column to 2 decimal places, apply the `en_US`
# locale and use all other defaults
expect_equal(
Expand Down
47 changes: 47 additions & 0 deletions tests/testthat/test-fmt_percent.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,53 @@ test_that("the `fmt_percent()` function works correctly in the HTML context", {
)
)

# Format the `num_1` column to 2 decimal places, force the sign
expect_equal(
(tab %>%
fmt_percent(
columns = num_1, decimals = 2, drop_trailing_zeros = TRUE,
scale_values = FALSE, force_sign = TRUE
) %>%
render_formats_test("html"))[["num_1"]],
c(
"+1,836.23&percnt;", "+2,763.39&percnt;", "+937.29&percnt;",
"+643&percnt;", "+212.23&percnt;", "0&percnt;", "&minus;23.24&percnt;"
)
)

# Expect that using `force_sign = TRUE` with `accounting = TRUE`
# will render values in accounting format
expect_equal(
(tab %>%
fmt_percent(
columns = num_1, decimals = 2, drop_trailing_zeros = TRUE,
scale_values = FALSE, accounting = TRUE, force_sign = TRUE
) %>%
render_formats_test("html"))[["num_1"]],
(tab %>%
fmt_percent(
columns = num_1, decimals = 2, drop_trailing_zeros = TRUE,
scale_values = FALSE, accounting = TRUE
) %>%
render_formats_test("html"))[["num_1"]]
)

# Format the `num_1` column to 2 decimal places, force the sign and
# define a pattern for decorating values
expect_equal(
(tab %>%
fmt_percent(
columns = num_1, decimals = 2, drop_trailing_zeros = TRUE,
pattern = "*{x}*", force_sign = TRUE
) %>%
render_formats_test("html"))[["num_1"]],
c(
"*+183,623&percnt;*", "*+276,339&percnt;*", "*+93,729&percnt;*",
"*+64,300&percnt;*", "*+21,223.2&percnt;*", "*0&percnt;*",
"*&minus;2,324&percnt;*"
)
)

# Format the `num_1` column to 2 decimal places, apply the `en_US`
# locale and use all other defaults
expect_equal(
Expand Down
Loading

0 comments on commit 9c788fd

Please sign in to comment.