Skip to content

Commit

Permalink
lazy_tt/format/style work
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 18, 2024
1 parent 1b1c5db commit 27e6afa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
28 changes: 14 additions & 14 deletions R/format_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {


Expand Down
18 changes: 17 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions inst/tinytest/test-format_tt.R
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 27e6afa

Please sign in to comment.