Skip to content

Commit

Permalink
format_tt doc and work
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 17, 2024
1 parent 44a80d1 commit 596f293
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 25 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ help: ## Display this help screen
check: ## check
Rscript -e "devtools::document();devtools::check()"

document: ## document
Rscript -e "devtools::document()"

test: ## test
Rscript -e "devtools::install();library(tinytable);tinytest::run_test_dir()"

Expand Down
41 changes: 25 additions & 16 deletions R/format_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
#' @param x A data frame 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).
#' @param num_mark_big Character to use as a thousands separator.
#' @param num_mark_dec Decimal mark character. Default is the global option 'OutDec'.
#' @param num_zero Logical; if TRUE, trailing zeros are kept.
#' @param url Logical; if TRUE, treats the column as a URL.
#' @param date A function to format Date columns. Defaults to ISO format.
#' @param bool A function to format logical columns. Defaults to title case.
#' @param ... Additional arguments are ignored.
#' @param other A function to format columns of other types. Defaults to identity (no formatting).
#' @inheritParams tt
#' @inheritParams style_tt
#'
#' @return A data frame with formatted columns.
#'
Expand All @@ -37,27 +38,32 @@ format_tt <- function(x = NULL,
output = NULL,
digits = NULL,
num_fmt = "significant",
num_mark_big = "",
num_mark_dec = getOption("OutDec"),
num_zero = TRUE,
num_mark_big = "",
num_mark_dec = getOption("OutDec", default = "."),
url = FALSE,
date = function(column) format(column, "%Y-%m-%d"),
bool = function(column) tools::toTitleCase(tolower(column)),
...
other = identity
) {

if (is.null(x)) {
out <- match.call()
class(out) <- c("tinytable_format_tt", class(out))
return(out)
if (inherits(x, "tinytable")) {
msg <- "`format_tt()` must be called *before* `tt()`. You must format your dataset before drawing a table."
}

assert_data_frame(x)
assert_integerish(digits, len = 1, null.ok = TRUE)
assert_choice(num_fmt, c("significant", "decimal", "scientific"))
assert_flag(num_zero)
assert_string(num_mark_big)
assert_string(num_mark_dec)
assert_flag(url)
assert_function(date)
assert_function(bool)
assert_function(identity)

output <- sanitize_output(output)

assert_choice(num_fmt, c("significant", "decimal", "scientific"))

# column index NULL or regex or integer vector
if (is.null(j)) {
j <- seq_len(ncol(x))
Expand All @@ -79,11 +85,12 @@ format_tt <- function(x = NULL,
x[[col]] <- date(x[[col]])

# numeric
} else if (is.numeric(x[[col]])) {
} else if (is.numeric(x[[col]]) && !is.null(digits)) {
if (num_fmt == "significant") {
x[[col]] <- formatC(x[[col]],
digits = digits, format = "g", drop0trailing = !num_zero,
big.mark = num_mark_big, decimal.mark = num_mark_dec)
x[[col]] <- format(x[[col]],
digits = digits, drop0trailing = !num_zero,
big.mark = num_mark_big, decimal.mark = num_mark_dec,
scientific = FALSE)
} else if (num_fmt == "decimal") {
x[[col]] <- formatC(x[[col]],
digits = digits, format = "f", drop0trailing = !num_zero,
Expand All @@ -93,10 +100,12 @@ format_tt <- function(x = NULL,
digits = digits, format = "e", drop0trailing = !num_zero,
big.mark = num_mark_big, decimal.mark = num_mark_dec)
}

} else {
x[[col]] <- other(x[[col]])
}

}

return(x)

}
9 changes: 9 additions & 0 deletions R/sanity.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,12 @@ assert_list <- function(x, named = FALSE, null.ok = FALSE, name = as.character(s
}
}
}


assert_function <- function(x, null.ok = FALSE, name = as.character(substitute(x))) {
if (isTRUE(null.ok) && is.null(x)) return(invisible(TRUE))
if (!is.function(x)) {
msg <- sprintf("`%s` must be a function.", name)
stop(msg, call. = FALSE)
}
}
2 changes: 2 additions & 0 deletions R/tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#'
#' @param x A data frame or data table to be rendered as a table.
#' @param output The format of the output table. Can be "html", "latex", or "markdown". If NULL, the format is automatically detected in Quarto or Rmarkdown documents.
#' @param digits Number of significant digits to keep for numeric variables. When `digits` is not `NULL`
#' @param align A string specifying the alignment of columns. Each character in the string corresponds to a column; 'l' for left, 'c' for center, and 'r' for right alignment. The length of the string must match the number of columns in `x`.
#' @param caption A string that will be used as the caption of the table.
#' @param width A numeric value between 0 and 1 indicating the proportion of the line width that the table should cover.
Expand Down Expand Up @@ -32,6 +33,7 @@
#' @export
tt <- function(x,
output = NULL,
digits = NULL,
align = NULL,
caption = NULL,
width = NULL,
Expand Down
7 changes: 4 additions & 3 deletions inst/tinytest/test-format_tt.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

# pkgload::load_all()
N <- 10
dat <- data.frame(
x = c(1.430, rnorm(N - 1, mean = 100000)),
y = as.Date(sample(1:1000, N)),
z = sample(c(TRUE, FALSE), N, replace = TRUE)
)
pkgload::load_all()
k = format_tt(num_fmt = "decimal", digits = 3, num_mark_big = " ", num_mark_dec = ",")
dat |>
format_tt(digits = 4) |>
tt()
14 changes: 8 additions & 6 deletions man/format_tt.Rd

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

3 changes: 3 additions & 0 deletions man/tt.Rd

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

0 comments on commit 596f293

Please sign in to comment.