From 58c570bf68e1b3637ba32d61e9e5d854b147a596 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Wed, 17 Jan 2024 12:26:07 -0500 Subject: [PATCH] docs --- R/format_tt.R | 22 ++++++++++++++++++++-- R/sanity.R | 12 ++++++++++++ inst/tinytest/test-format_tt.R | 4 ++++ man/format_tt.Rd | 2 +- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/R/format_tt.R b/R/format_tt.R index f007876b..7a6250ce 100644 --- a/R/format_tt.R +++ b/R/format_tt.R @@ -8,7 +8,7 @@ #' It allows various formatting options like significant digits, decimal points, and scientific notation. #' It also includes custom formatting for date and boolean values. #' -#' @param x A data frame to be formatted. +#' @param x A data frame or a vector to be formatted. #' @param digits Number of significant digits or decimal places. #' @param num_fmt The format for numeric values; one of 'significant', 'decimal', or 'scientific'. #' @param num_zero Logical; if TRUE, trailing zeros are kept in "decimal" format (but not in "significant" format). @@ -51,6 +51,19 @@ format_tt <- function(x = NULL, msg <- "`format_tt()` must be called *before* `tt()`. You must format your dataset before drawing a table." } + if (isTRUE(check_atomic_vector(x))) { + atomic_vector <- TRUE + x <- data.frame(tinytable = x) + j <- 1 + } else { + atomic_vector <- FALSE + } + + if (!inherits(x, "data.frame")) { + msg <- "`x` must be a data frame or an atomic vector." + stop(msg, call. = FALSE) + } + assert_data_frame(x) assert_integerish(digits, len = 1, null.ok = TRUE) assert_choice(num_fmt, c("significant", "decimal", "scientific")) @@ -107,5 +120,10 @@ format_tt <- function(x = NULL, } - return(x) + if (isTRUE(atomic_vector)) { + return(x[[1]]) + } else { + return(x) + } + } diff --git a/R/sanity.R b/R/sanity.R index fd312537..9897c1d0 100644 --- a/R/sanity.R +++ b/R/sanity.R @@ -191,3 +191,15 @@ assert_function <- function(x, null.ok = FALSE, name = as.character(substitute(x stop(msg, call. = FALSE) } } + +check_atomic_vector<- function(x, null.ok = FALSE, name = as.character(substitute(x))) { + if (isTRUE(null.ok) && is.null(x)) return(invisible(TRUE)) + flag <- is.atomic(x) && is.vector(x) && !is.list(x) + if (flag) { + out <- TRUE + } else { + out <- sprintf("`%s` must be an atomic vector.", name) + } + return(out) +} + diff --git a/inst/tinytest/test-format_tt.R b/inst/tinytest/test-format_tt.R index d343fde6..b07a493d 100644 --- a/inst/tinytest/test-format_tt.R +++ b/inst/tinytest/test-format_tt.R @@ -5,6 +5,10 @@ dat <- data.frame( y = as.Date(sample(1:1000, N)), z = sample(c(TRUE, FALSE), N, replace = TRUE) ) + +# pkgload::load_all() dat |> format_tt(digits = 4) |> tt() + +format_tt(dat$x, digits = 1) diff --git a/man/format_tt.Rd b/man/format_tt.Rd index 7de22778..d022cf13 100644 --- a/man/format_tt.Rd +++ b/man/format_tt.Rd @@ -20,7 +20,7 @@ format_tt( ) } \arguments{ -\item{x}{A data frame to be formatted.} +\item{x}{A data frame or a vector to be formatted.} \item{j}{Column indices where the styling should be applied. Can be a single value, a vector, or a Perl-style regular expression applied to column names of the original data frame. If \code{colspan} is used, \code{j} must be of length 1.}