From 27e6afabcd0e42e6dfd8c2ddf2dea177db58dfeb Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Thu, 18 Jan 2024 11:50:12 -0500 Subject: [PATCH] lazy_tt/format/style work --- R/format_tt.R | 28 ++++++++++++++-------------- R/utils.R | 18 +++++++++++++++++- inst/tinytest/test-format_tt.R | 23 ++++++++++++----------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/R/format_tt.R b/R/format_tt.R index 0fd06804..8bddfe1e 100644 --- a/R/format_tt.R +++ b/R/format_tt.R @@ -79,20 +79,20 @@ format_tt <- function(x = NULL, return(out) } -format_tt_lazy <- function(x = NULL, - j = NULL, - output = NULL, - digits = getOption("digits"), - num_fmt = "significant", - num_zero = TRUE, - num_suffix = FALSE, - num_mark_big = "", - num_mark_dec = getOption("OutDec", default = "."), - sprintf = NULL, - url = FALSE, - date = "%Y-%m-%d", - bool = function(column) tools::toTitleCase(tolower(column)), - other = identity +format_tt_lazy <- function(x, + j, + output, + digits, + num_fmt, + num_zero, + num_suffix, + num_mark_big, + num_mark_dec, + sprintf, + url, + date, + bool, + other ) { diff --git a/R/utils.R b/R/utils.R index f2e66e0a..7f9ba41e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -35,8 +35,24 @@ meta <- function(x, get, set) { build_tt <- function(x) { m <- meta(x) - out <- eval(meta(x, "lazy_tt")) + out <- x + # format data before drawing the table + for (l in m$lazy_format) { + tmp <- out + class(tmp) <- "data.frame" + l[["x"]] <- tmp + out <- eval(l) + } + + # draw the table + lazy_tt <- meta(x, "lazy_tt") + lazy_tt[["x"]] <- out + out <- eval(lazy_tt) + + # TODO: group the table + + # style the table for (l in m$lazy_style) { l[["x"]] <- out out <- eval(l) diff --git a/inst/tinytest/test-format_tt.R b/inst/tinytest/test-format_tt.R index d22dc143..b481f222 100644 --- a/inst/tinytest/test-format_tt.R +++ b/inst/tinytest/test-format_tt.R @@ -1,14 +1,15 @@ -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) -) - -Q +# 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) +# ) +# +# Q pkgload::load_all() k = dat |> tt("latex") |> - format_tt(digits = 2, num_suffix = TRUE) -knitr::knit_print(k) + style_tt(color = "orange") |> + format_tt(j = 1, digits = 10, num_fmt = "decimal") |> + format_tt(j = 2, date = "%Y") print(k)