Skip to content

Commit

Permalink
changed function name
Browse files Browse the repository at this point in the history
added data frame test
changed the no rows from a warning to a stop
updated documentation
  • Loading branch information
mzayeddfe committed Oct 14, 2024
1 parent a0824c3 commit 189b6cc
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 43 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export(get_clean_sql)
export(get_ons_api_data)
export(pretty_filesize)
export(pretty_num)
export(pretty_table)
export(pretty_num_table)
export(pretty_time_taken)
export(round_five_up)
export(toggle_message)
Expand Down
40 changes: 24 additions & 16 deletions R/pretty.R
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ pretty_num <- function(
return(unlist(result))
}

#' Format a data frame with `dfeR::pretty_num`.
#' Format a data frame with `dfeR::pretty_num()`.
#'
#' You can format number and character values in a data frame
#' by passing arguments to `dfeR::pretty_num`.
#' by passing arguments to `dfeR::pretty_num()`.
#' Use parameters `include_columns` or `exclude_columns`
#' to specify columns for formatting.
#'
Expand All @@ -330,11 +330,11 @@ pretty_num <- function(
#' If `NULL` (default), no columns will be excluded.
#' If both `include_columns` and `exclude_columns` are provided
#' , `include_columns` takes precedence.
#' @param ... Additional arguments passed to `dfeR::pretty_num`
#' @param ... Additional arguments passed to `dfeR::pretty_num()`
#' , such as `dp` (decimal places)
#' for controlling the number of decimal points.
#'
#' @return A data frame with columns formatted using `dfeR::pretty_num`.
#' @return A data frame with columns formatted using `dfeR::pretty_num()`.
#'
#' @details
#' The function first checks if any columns are specified for inclusion
Expand All @@ -354,25 +354,33 @@ pretty_num <- function(
#' )
#'
#' # Apply formatting to all columns
#' pretty_table(df, dp = 2)
#' pretty_num_table(df, dp = 2)
#'
#' # Apply formatting to only selected columns
#' pretty_table(df, include_columns = c("a"), dp = 2)
#' pretty_num_table(df, include_columns = c("a"), dp = 2)
#'
#' # Apply formatting to all columns except specified ones
#' pretty_table(df, exclude_columns = c("b"), dp = 2)
#' pretty_num_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,
...) {
#' pretty_num_table(df, alt_na = "[z]", exclude_columns = c("b"), dp = 2)
#'
pretty_num_table <- function(data,
include_columns = NULL,
exclude_columns = NULL,
...) {
# Check data is a data frame and throw error if not
if (!is.data.frame(data)) {
stop(paste0(
"Data has the class ", class(data),
", data must be a data.frame object"
))
}

# Check if the data frame has rows - if not, stop the process
if (nrow(data) < 1) {
warning("Data frame is empty or contains no rows.")
stop("Data frame is empty or contains no rows.")
}

# Determine which columns to include based on the provided parameters
Expand All @@ -392,11 +400,11 @@ pretty_table <- function(data,
)
} else {
# if none of the previous conditions are met
# , all columns are assigned to cols_to_include
# all columns are assigned to cols_to_include
cols_to_include <- names(data)
}

# Apply pretty_num formatting to the selected columns
# Apply pretty_num() formatting to the selected columns
data %>%
dplyr::mutate(dplyr::across(
.cols = dplyr::all_of(cols_to_include),
Expand Down
2 changes: 1 addition & 1 deletion man/pretty_filesize.Rd

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

2 changes: 1 addition & 1 deletion man/pretty_num.Rd

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

22 changes: 11 additions & 11 deletions man/pretty_table.Rd → man/pretty_num_table.Rd

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

2 changes: 1 addition & 1 deletion man/pretty_time_taken.Rd

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

Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ df <- data.frame(


test_that("prettifies tables", {
expect_equal(pretty_table(df), data.frame(
expect_equal(pretty_num_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)),
b = c("£11.20", "£45.69", "-£78.50"),
c = c("X", "Y", "Z")
))
expect_equal(
pretty_num_table(df, gbp = TRUE, exclude_columns = "c"),
data.frame(
a = c("£2.59", "-£5.89", as.double(NA)),
b = c("£11.20", "£45.69", "-£78.50"),
c = c("X", "Y", "Z")
)
)


expect_equal(
pretty_table(df,
pretty_num_table(df,
suffix = "%", dp = 1, nsmall = 2,
exclude_columns = c("b", "c")
),
Expand All @@ -34,7 +37,7 @@ test_that("prettifies tables", {
)

expect_equal(
pretty_table(df,
pretty_num_table(df,
alt_na = "[z]", dp = -1,
include_columns = c("a", "b")
),
Expand All @@ -46,7 +49,7 @@ test_that("prettifies tables", {
)

expect_equal(
pretty_table(df,
pretty_num_table(df,
alt_na = "", dp = 2,
prefix = "+/-", suffix = "g", include_columns = "a"
),
Expand All @@ -59,7 +62,7 @@ test_that("prettifies tables", {


expect_equal(
pretty_table(df,
pretty_num_table(df,
dp = 2,
include_columns = "a", exclude_columns = "b"
),
Expand All @@ -80,6 +83,25 @@ df <- data.frame(
c = character()
)

test_that("pretty_table with empty data frames", {
expect_warning(pretty_table(df), "Data frame is empty or contains no rows.")
test_that("pretty_num_table with empty data frames", {
expect_error(pretty_num_table(df), "Data frame is empty or contains no rows.")
})

# test non data frame objects

test_that("test non data frames", {
expect_error(
pretty_num_table(1.12),
"Data has the class numeric, data must be a data.frame object"
)

expect_error(
pretty_num_table("a"),
"Data has the class character, data must be a data.frame object"
)

expect_error(
pretty_num_table(c("a", 1.2)),
"Data has the class character, data must be a data.frame object"
)
})

0 comments on commit 189b6cc

Please sign in to comment.