Skip to content

Commit

Permalink
Fixes #32 set a more consistent behaviour for formatNumerics (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
pchelle authored Feb 7, 2022
1 parent 0572374 commit c6a86c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
8 changes: 4 additions & 4 deletions R/formatNumerics.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#' @title formatNumerics
#' @description Render numeric values of an `object` using the specified format:
#' @description Render numeric values of an `object` as character using the specified format:
#' - If `object` is a data.frame or a list, `formatNumerics` applies on each of its fields
#' - If `object` is of type character or integer, `formatNumerics` leaves the values as is
#' - If `object` is of type character or integer, `formatNumerics` renders the values as is
#' - If `object` is of type numeric, `formatNumerics` applies the defined format
#'
#' @param object An R object such as a list, a data.frame, character or numeric values.
#' @param digits Number of decimal digits to render
#' @param scientific Logical value defining if scientific writing is rendered
#'
#' @return Numeric values are rendered as character values. If `object` was a
#' @return Numeric values are rendered as character values. If `object` is a
#' data.frame or a list, a data.frame or list is returned with numeric values
#' rendered as character values.
#'
Expand All @@ -26,7 +26,7 @@ formatNumerics <- function(object, digits = 2, scientific = FALSE) {

# Return integer as is before they are assumed as numeric
if (is.integer(object)) {
return(object)
return(as.character(object))
}

# Method for numeric values
Expand Down
6 changes: 3 additions & 3 deletions man/formatNumerics.Rd

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

22 changes: 19 additions & 3 deletions tests/testthat/test-formatNumerics.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
test_that("formatNumerics work as expected", {
test_that("formatNumerics returns character for integer, numeric or character objects", {
# integer
expect_type(formatNumerics(2L, digits = 1, scientific = TRUE), "character")
# numeric
expect_type(formatNumerics(2, digits = 1, scientific = TRUE), "character")
# character
expect_type(formatNumerics("2", digits = 1, scientific = TRUE), "character")
})


test_that("formatNumerics works as expected", {
# integer and character as is
expect_equal(formatNumerics(2L, digits = 1, scientific = TRUE), "2")
expect_equal(formatNumerics("2", digits = 1, scientific = TRUE), "2")
# numeric is formated
expect_equal(formatNumerics(2, digits = 1, scientific = TRUE), "2.0e+00")

# vector
x <- formatNumerics(log(c(12, 15, 0.3)), digits = 1, scientific = TRUE)
expect_equal(x, c("2.5e+00", "2.7e+00", "-1.2e+00"))

y <- formatNumerics(c(12L, 15L, 3L))
expect_equal(y, c(12L, 15L, 3L))
expect_equal(y, c("12", "15", "3"))

# dataframe
# data.frame
df <- data.frame(
parameter = c("a", "b", "c"),
value = c(1, 110.4, 6.666),
Expand Down

0 comments on commit c6a86c4

Please sign in to comment.