Skip to content

Commit

Permalink
format_value() should consider "options(OutDec=',')" for separator fo…
Browse files Browse the repository at this point in the history
…rmatting (#957)

Fixes #193
  • Loading branch information
strengejacke authored Nov 4, 2024
1 parent e023a26 commit 2f9411f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
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 @@ test_that("format_value", {
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"))
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

0 comments on commit 2f9411f

Please sign in to comment.