Skip to content

Commit

Permalink
adding documentation, testing and extra comments for pretty_table
Browse files Browse the repository at this point in the history
an extra documentation line for pretty_num came up when running workflows
  • Loading branch information
mzayeddfe committed Oct 9, 2024
1 parent 3d67fa5 commit 0b4ce75
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 24 deletions.
23 changes: 12 additions & 11 deletions R/pretty.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,15 @@ pretty_num <- function(
return(unlist(result))
}

#' Format Numeric Columns in a Data Frame with Pretty Numbers
#' Format columns in a Data Frame with `dfeR::pretty_num`.
#'
#' This function formats numeric columns in data frames with `dfeR::pretty_num`.
#' Use parameters `include_columns` or `exclude_columns`
#' to specify columns for formatting
#' and pass arguments to `dfeR::pretty_num` for number formatting
#'
#' @param data A data frame containing the numeric columns to be formatted.
#' @param include_columns A character vector specifying which columns to format.
#' If `NULL` (default), all numeric columns will be considered for formatting.
#' If `NULL` (default), all columns will be considered for formatting.
#' @param exclude_columns A character vector specifying columns to exclude
#' from formatting.
#' If `NULL` (default), no columns will be excluded.
Expand All @@ -333,16 +332,14 @@ pretty_num <- function(
#' , such as `dp` (decimal places)
#' for controlling the number of decimal points.
#'
#' @return A data frame with the specified numeric columns
#' formatted using `dfeR::pretty_num`.
#' Non-numeric columns and excluded numeric columns (if any) are left unchanged.
#' @return A data frame with columns formatted using `dfeR::pretty_num`.
#'
#' @details
#' The function first checks if any columns are specified for inclusion
#' via `include_columns`.
#' If none are provided, it checks if columns are specified for exclusion
#' via `exclude_columns`.
#' If neither is specified, all numeric columns in the data frame are formatted.
#' If neither is specified, all columns in the data frame are formatted.
#'
#' @examples
#' # Example data frame
Expand All @@ -352,15 +349,19 @@ pretty_num <- function(
#' c = c("A", "B", "C")
#' )
#'
#' # Apply formatting to all numeric columns
#' # Apply formatting to all columns
#' pretty_table(df, dp = 2)
#'
#' # Apply formatting to only selected columns
#' pretty_table(df, include_columns = c("a"), dp = 2)
#'
#' # Apply formatting to all numeric columns except specified ones
#' # Apply formatting to all columns except specified ones
#' pretty_table(df, exclude_columns = c("b"), dp = 2)
#'
#' # Apply formatting to all columns except specified ones and
#' # provide alternative value for NAs
#' pretty_table(df, alt_na = "[z]", exclude_columns = c("b"), dp = 2)
#'
pretty_table <- function(data,
include_columns = NULL,
exclude_columns = NULL,
Expand Down Expand Up @@ -391,10 +392,10 @@ pretty_table <- function(data,
cols_to_include <- names(data)
}

# Apply pretty_num formatting to the selected numeric columns
# Apply pretty_num formatting to the selected columns
data %>%
dplyr::mutate(dplyr::across(
.cols = dplyr::all_of(cols_to_include),
~ pretty_num(.,...)
~ pretty_num(., ...)
))
}
3 changes: 2 additions & 1 deletion man/pretty_num.Rd

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

61 changes: 61 additions & 0 deletions man/pretty_table.Rd

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

75 changes: 63 additions & 12 deletions tests/testthat/test-pretty_table.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# testing pretty table
# create data frame for testing
df <- data.frame(
a = c(2.589, -5.8937, "c"),
b = c(11.19875, 45.6894, -78.4985),
Expand All @@ -6,12 +8,11 @@ df <- data.frame(


test_that("prettifies tables", {

expect_equal(pretty_table(df), 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_table(df, gbp = TRUE, exclude_columns = "c"), data.frame(
a = c("£2.59", "-£5.89", as.double(NA)),
Expand All @@ -20,15 +21,65 @@ test_that("prettifies tables", {
))


expect_equal(pretty_table(df, suffix = "%", dp = 1,nsmall=2,exclude_columns = c("b","c"))
, data.frame(
a = c("2.60%", "-5.90%", as.double(NA)),
b = c(11.19875, 45.6894, -78.4985),
c = c("X", "Y", "Z")
))
expect_equal(
pretty_table(df,
suffix = "%", dp = 1, nsmall = 2,
exclude_columns = c("b", "c")
),
data.frame(
a = c("2.60%", "-5.90%", as.double(NA)),
b = c(11.19875, 45.6894, -78.4985),
c = c("X", "Y", "Z")
)
)

expect_equal(
pretty_table(df,
alt_na = "[z]", dp = -1,
include_columns = c("a", "b")
),
data.frame(
a = c("0", "-10", "[z]"),
b = c("10", "50", "-80"),
c = c("X", "Y", "Z")
)
)

expect_equal(
pretty_table(df,
alt_na = "", dp = 2,
prefix = "+/-", suffix = "g", include_columns = "a"
),
data.frame(
a = c("+2.59g", "-5.89g", ""),
b = c(11.19875, 45.6894, -78.4985),
c = c("X", "Y", "Z")
)
)


expect_equal(
pretty_table(df,
dp = 2,
include_columns = "a", exclude_columns = "b"
),
data.frame(
a = c("2.59", "-5.89", as.double(NA)),
b = c(11.19875, 45.6894, -78.4985),
c = c("X", "Y", "Z")
)
)
})

# test empty data frame

# create empty data frame for testing
df <- data.frame(
a = character(),
b = character(),
c = character()
)

expect_equal(pretty_table(df, alt_na="[z]", dp = -1, exclude_columns = "c") , data.frame(
a = c("0", "-10", "[z]"),
b = c("10", "50", "-80"),
c = c("X", "Y", "Z")))
test_that("pretty_table with empty data frames", {
expect_warning(pretty_table(df), "Data frame is empty or contains no rows.")
})

0 comments on commit 0b4ce75

Please sign in to comment.