Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 17, 2024
1 parent e2b6d1e commit 58c570b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
22 changes: 20 additions & 2 deletions R/format_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -107,5 +120,10 @@ format_tt <- function(x = NULL,

}

return(x)
if (isTRUE(atomic_vector)) {
return(x[[1]])
} else {
return(x)
}

}
12 changes: 12 additions & 0 deletions R/sanity.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

4 changes: 4 additions & 0 deletions inst/tinytest/test-format_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion man/format_tt.Rd

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

0 comments on commit 58c570b

Please sign in to comment.