From 189b6cc5be12003243e64b1f15f0d1836244b291 Mon Sep 17 00:00:00 2001 From: ZAYED Date: Mon, 14 Oct 2024 09:18:26 +0100 Subject: [PATCH] changed function name added data frame test changed the no rows from a warning to a stop updated documentation --- NAMESPACE | 2 +- R/pretty.R | 40 +++++++++------- man/pretty_filesize.Rd | 2 +- man/pretty_num.Rd | 2 +- man/{pretty_table.Rd => pretty_num_table.Rd} | 22 ++++----- man/pretty_time_taken.Rd | 2 +- ...pretty_table.R => test-pretty_num_table.R} | 46 ++++++++++++++----- 7 files changed, 73 insertions(+), 43 deletions(-) rename man/{pretty_table.Rd => pretty_num_table.Rd} (80%) rename tests/testthat/{test-pretty_table.R => test-pretty_num_table.R} (60%) diff --git a/NAMESPACE b/NAMESPACE index a66005c..2c1339e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/pretty.R b/R/pretty.R index 83f2e2e..a837403 100644 --- a/R/pretty.R +++ b/R/pretty.R @@ -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. #' @@ -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 @@ -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 @@ -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), diff --git a/man/pretty_filesize.Rd b/man/pretty_filesize.Rd index 3b4ea65..1d761a8 100644 --- a/man/pretty_filesize.Rd +++ b/man/pretty_filesize.Rd @@ -42,7 +42,7 @@ pretty_filesize(10^9) Other prettying: \code{\link{pretty_num}()}, -\code{\link{pretty_table}()}, +\code{\link{pretty_num_table}()}, \code{\link{pretty_time_taken}()} } \concept{prettying} diff --git a/man/pretty_num.Rd b/man/pretty_num.Rd index 6257729..bbe78ed 100644 --- a/man/pretty_num.Rd +++ b/man/pretty_num.Rd @@ -84,7 +84,7 @@ pretty_num(vector, alt_na = "z") Other prettying: \code{\link{pretty_filesize}()}, -\code{\link{pretty_table}()}, +\code{\link{pretty_num_table}()}, \code{\link{pretty_time_taken}()} } \concept{prettying} diff --git a/man/pretty_table.Rd b/man/pretty_num_table.Rd similarity index 80% rename from man/pretty_table.Rd rename to man/pretty_num_table.Rd index 654301c..4dbb009 100644 --- a/man/pretty_table.Rd +++ b/man/pretty_num_table.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pretty.R -\name{pretty_table} -\alias{pretty_table} -\title{Format a data frame with \code{dfeR::pretty_num}.} +\name{pretty_num_table} +\alias{pretty_num_table} +\title{Format a data frame with \code{dfeR::pretty_num()}.} \usage{ -pretty_table(data, include_columns = NULL, exclude_columns = NULL, ...) +pretty_num_table(data, include_columns = NULL, exclude_columns = NULL, ...) } \arguments{ \item{data}{A data frame containing the columns to be formatted.} @@ -18,16 +18,16 @@ If \code{NULL} (default), no columns will be excluded. If both \code{include_columns} and \code{exclude_columns} are provided , \code{include_columns} takes precedence.} -\item{...}{Additional arguments passed to \code{dfeR::pretty_num} +\item{...}{Additional arguments passed to \code{dfeR::pretty_num()} , such as \code{dp} (decimal places) for controlling the number of decimal points.} } \value{ -A data frame with columns formatted using \code{dfeR::pretty_num}. +A data frame with columns formatted using \code{dfeR::pretty_num()}. } \description{ You can format number and character values in a data frame -by passing arguments to \code{dfeR::pretty_num}. +by passing arguments to \code{dfeR::pretty_num()}. Use parameters \code{include_columns} or \code{exclude_columns} to specify columns for formatting. } @@ -47,17 +47,17 @@ df <- data.frame( ) # 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_num_table(df, alt_na = "[z]", exclude_columns = c("b"), dp = 2) } \seealso{ diff --git a/man/pretty_time_taken.Rd b/man/pretty_time_taken.Rd index 31b9e77..0bee42b 100644 --- a/man/pretty_time_taken.Rd +++ b/man/pretty_time_taken.Rd @@ -45,6 +45,6 @@ pretty_time_taken(start, end) Other prettying: \code{\link{pretty_filesize}()}, \code{\link{pretty_num}()}, -\code{\link{pretty_table}()} +\code{\link{pretty_num_table}()} } \concept{prettying} diff --git a/tests/testthat/test-pretty_table.R b/tests/testthat/test-pretty_num_table.R similarity index 60% rename from tests/testthat/test-pretty_table.R rename to tests/testthat/test-pretty_num_table.R index 47f559d..3c89f3e 100644 --- a/tests/testthat/test-pretty_table.R +++ b/tests/testthat/test-pretty_num_table.R @@ -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") ), @@ -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") ), @@ -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" ), @@ -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" ), @@ -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" + ) })