Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format_value() should consider "options(OutDec=',')" for separator formatting #957

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
* `export_table()` can now split tables into more than three tables when
`table_width` is used (formerly, the maximum number of split tables was three).

* `format_value()` gains a `decimal_point` argument, to change the decimal point
in output conversion.

## Bug fix

* `clean_parameters()` now uses the correct labels for the random effects
Expand Down
11 changes: 11 additions & 0 deletions R/format_value.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#' @param style_negative A string that determines the style of negative numbers.
#' May be `"hyphen"` (default), `"minus"` for a proper Unicode minus symbol or
#' `"parens"` to wrap the number in parentheses.
#' @param decimal_point Character string containing a single character that
#' is used as decimal point in output conversions.
#' @param ... Arguments passed to or from other methods.
#'
#'
Expand All @@ -49,6 +51,7 @@
#' format_value(c(0.0045, 0.12, 0.34), digits = "scientific")
#' format_value(c(0.0045, 0.12, 0.34), digits = "scientific2")
#' format_value(c(0.045, 0.12, 0.34), lead_zero = FALSE)
#' format_value(c(0.0045, 0.12, 0.34), decimal_point = ",")
#'
#' # default
#' format_value(c(0.0045, 0.123, 0.345))
Expand Down Expand Up @@ -80,6 +83,7 @@ format_value.data.frame <- function(x,
lead_zero = TRUE,
style_positive = "none",
style_negative = "hyphen",
decimal_point = getOption("OutDec"),
...) {
as.data.frame(sapply(
x,
Expand All @@ -93,6 +97,7 @@ format_value.data.frame <- function(x,
lead_zero = lead_zero,
style_positive = style_positive,
style_negative = style_negative,
decimal_point = decimal_point,
simplify = FALSE
))
}
Expand All @@ -110,6 +115,7 @@ format_value.numeric <- function(x,
lead_zero = TRUE,
style_positive = "none",
style_negative = "hyphen",
decimal_point = getOption("OutDec"),
...) {
# check input
style_positive <- validate_argument(style_positive, c("none", "plus", "space"))
Expand Down Expand Up @@ -167,6 +173,11 @@ format_value.numeric <- function(x,
} else if (style_negative == "parens") {
out[negatives] <- gsub("-(.*)", "\\(\\1\\)", out[negatives])
}

# decimal points
if (!is.null(decimal_point) && !identical(decimal_point, ".")) {
out <- gsub(".", decimal_point, out, fixed = TRUE)
}
}

out
Expand Down
6 changes: 6 additions & 0 deletions man/format_value.Rd

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

2 changes: 2 additions & 0 deletions tests/testthat/test-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
expect_identical(format_value(4.0, protect_integers = TRUE), "4")
expect_identical(format_value(0, protect_integers = TRUE), "0")
expect_identical(format_value(0), "0.00")
expect_identical(format_value(0, decimal_point = ","), "0,00")
expect_identical(format_value(1234565789101112), "1.23e+15")
expect_identical(format_value(1234565789101112, protect_integers = TRUE), "1234565789101112")
expect_identical(format_value(0.0000000123), "1.23e-08")
expect_identical(format_value(0.0000000123, zap_small = TRUE), "0.00")
expect_identical(format_value(0.0000000123, digits = 8), "0.00000001")
expect_identical(format_value(c(0.012, 0.45, -0.03), lead_zero = FALSE), c(".01", ".45", "-.03"))
expect_identical(format_value(c(1.012, 0.45, -0.03), lead_zero = FALSE), c("1.01", ".45", "-.03"))
expect_identical(format_value(c(1.012, 0.45, -0.03), lead_zero = FALSE, decimal_point = ","), c("1,01", ",45", "-,03"))

Check warning on line 15 in tests/testthat/test-format.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-format.R,line=15,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 121 characters.
expect_identical(format_value(c(0.45, -0.03), style_positive = "plus"), c("+0.45", "-0.03"))
expect_identical(format_value(c(0.45, -0.03), style_positive = "plus", lead_zero = FALSE), c("+.45", "-.03"))
expect_equal(
Expand Down Expand Up @@ -42,7 +44,7 @@
test_that("format_ci", {
expect_identical(
format_ci(c(123, 123, 123, 123), c(123, 12345, 123456, 123456789012), width = "auto"),
c("95% CI [123.00, 123.00]", "95% CI [123.00, 12345.00]", "95% CI [123.00, 1.23e+05]", "95% CI [123.00, 1.23e+11]")

Check warning on line 47 in tests/testthat/test-format.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-format.R,line=47,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 121 characters.
)
expect_identical(
format_ci(c(123, 123, 123, 123), c(123, 12345, 123456, 123456789012), width = "auto", digits = 5),
Expand Down
Loading