Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 17, 2024
1 parent 3df02c0 commit 822efd0
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 63 deletions.
125 changes: 62 additions & 63 deletions R/style_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ style_tt_lazy <- function (x,
assert_style_tt <- function (x,
i,
j,
ival,
jval,
bold,
italic,
monospace,
Expand All @@ -157,67 +155,68 @@ assert_style_tt <- function (x,

m <- meta(x)

# assert_integerish(colspan, len = 1, lower = 1, null.ok = TRUE)
# assert_integerish(ival, lower = 1 - meta(x)$nhead, upper = meta(x)$nrows, name = "i")
# assert_integerish(jval, lower = 1, upper = meta(x)$ncols, name = "j")
# assert_string(width, null.ok = TRUE)
# assert_choice(align, c("c", "l", "r"), null.ok = TRUE)
# assert_numeric(indent, len = 1, lower = 0)
# assert_character(background, null.ok = TRUE)
# assert_character(color, null.ok = TRUE)
# assert_integerish(fontsize, null.ok = TRUE)
# assert_logical(bold)
# assert_logical(italic)
# assert_logical(monospace)
# assert_logical(underline)
# assert_logical(strikeout)
# assert_character(bootstrap_css, null.ok = TRUE)
# assert_string(bootstrap_css_rule, null.ok = TRUE)
#
# # 1
# if (is.null(i) && is.null(j)) {
# assert_length(color, len = 1, null.ok = TRUE)
# assert_length(background, len = 1, null.ok = TRUE)
# assert_length(fontsize, len = 1, null.ok = TRUE)
# assert_length(bold, len = 1)
# assert_length(italic, len = 1)
# assert_length(monospace, len = 1)
# assert_length(underline, len = 1)
# assert_length(strikeout, len = 1)
#
# # 1 or #rows
# } else if (!is.null(i) && is.null(j)) {
# assert_length(color, len = c(1, length(ival)), null.ok = TRUE)
# assert_length(background, len = c(1, length(ival)), null.ok = TRUE)
# assert_length(fontsize, len = c(1, length(ival)), null.ok = TRUE)
# assert_length(bold, len = c(1, length(ival)))
# assert_length(italic, len = c(1, length(ival)))
# assert_length(monospace, len = c(1, length(ival)))
# assert_length(underline, len = c(1, length(ival)))
# assert_length(strikeout, len = c(1, length(ival)))
#
# # 1 or #cols
# } else if (is.null(i) && !is.null(j)) {
# assert_length(color, len = c(1, length(jval)), null.ok = TRUE)
# assert_length(background, len = c(1, length(jval)), null.ok = TRUE)
# assert_length(fontsize, len = c(1, length(jval)), null.ok = TRUE)
# assert_length(bold, len = c(1, length(jval)))
# assert_length(italic, len = c(1, length(jval)))
# assert_length(monospace, len = c(1, length(jval)))
# assert_length(underline, len = c(1, length(jval)))
# assert_length(strikeout, len = c(1, length(jval)))
#
# # 1 or #cells
# } else if (!is.null(i) && !is.null(j)) {
# assert_length(color, len = c(1, length(ival) * length(jval)), null.ok = TRUE)
# assert_length(background, len = c(1, length(ival) * length(jval)), null.ok = TRUE)
# assert_length(fontsize, len = c(1, length(ival) * length(jval)), null.ok = TRUE)
# assert_length(bold, len = c(1, length(ival) * length(jval)))
# assert_length(italic, len = c(1, length(ival) * length(jval)))
# assert_length(monospace, len = c(1, length(ival) * length(jval)))
# assert_length(underline, len = c(1, length(ival) * length(jval)))
# assert_length(strikeout, len = c(1, length(ival) * length(jval)))
# }
assert_integerish(colspan, len = 1, lower = 1, null.ok = TRUE)
assert_string(width, null.ok = TRUE)
assert_choice(align, c("c", "l", "r"), null.ok = TRUE)
assert_numeric(indent, len = 1, lower = 0)
assert_character(background, null.ok = TRUE)
assert_character(color, null.ok = TRUE)
assert_integerish(fontsize, null.ok = TRUE)
assert_logical(bold)
assert_logical(italic)
assert_logical(monospace)
assert_logical(underline)
assert_logical(strikeout)
assert_character(bootstrap_css, null.ok = TRUE)
assert_string(bootstrap_css_rule, null.ok = TRUE)

ival <- if (is.null(i)) meta(x, "nrows") else i
jval <- if (is.null(j)) meta(x, "ncols") else j

# 1
if (is.null(i) && is.null(j)) {
assert_length(color, len = 1, null.ok = TRUE)
assert_length(background, len = 1, null.ok = TRUE)
assert_length(fontsize, len = 1, null.ok = TRUE)
assert_length(bold, len = 1)
assert_length(italic, len = 1)
assert_length(monospace, len = 1)
assert_length(underline, len = 1)
assert_length(strikeout, len = 1)

# 1 or #rows
} else if (!is.null(i) && is.null(j)) {
assert_length(color, len = c(1, length(ival)), null.ok = TRUE)
assert_length(background, len = c(1, length(ival)), null.ok = TRUE)
assert_length(fontsize, len = c(1, length(ival)), null.ok = TRUE)
assert_length(bold, len = c(1, length(ival)))
assert_length(italic, len = c(1, length(ival)))
assert_length(monospace, len = c(1, length(ival)))
assert_length(underline, len = c(1, length(ival)))
assert_length(strikeout, len = c(1, length(ival)))

# 1 or #cols
} else if (is.null(i) && !is.null(j)) {
assert_length(color, len = c(1, length(jval)), null.ok = TRUE)
assert_length(background, len = c(1, length(jval)), null.ok = TRUE)
assert_length(fontsize, len = c(1, length(jval)), null.ok = TRUE)
assert_length(bold, len = c(1, length(jval)))
assert_length(italic, len = c(1, length(jval)))
assert_length(monospace, len = c(1, length(jval)))
assert_length(underline, len = c(1, length(jval)))
assert_length(strikeout, len = c(1, length(jval)))

# 1 or #cells
} else if (!is.null(i) && !is.null(j)) {
assert_length(color, len = c(1, length(ival) * length(jval)), null.ok = TRUE)
assert_length(background, len = c(1, length(ival) * length(jval)), null.ok = TRUE)
assert_length(fontsize, len = c(1, length(ival) * length(jval)), null.ok = TRUE)
assert_length(bold, len = c(1, length(ival) * length(jval)))
assert_length(italic, len = c(1, length(ival) * length(jval)))
assert_length(monospace, len = c(1, length(ival) * length(jval)))
assert_length(underline, len = c(1, length(ival) * length(jval)))
assert_length(strikeout, len = c(1, length(ival) * length(jval)))
}
}


Binary file added inst/tinytest/_tinysnapshot/html-heatmap.rds
Binary file not shown.
Binary file added inst/tinytest/_tinysnapshot/html-issue58.rds
Binary file not shown.
Binary file added inst/tinytest/_tinysnapshot/html-striped.rds
Binary file not shown.
Binary file not shown.
Binary file not shown.
51 changes: 51 additions & 0 deletions inst/tinytest/test-html.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

source("helpers.R")
using("tinysnapshot")


clean <- function(x) {
x <- knitr::knit_print(x)
x <- gsub("tinytable_\\w+\\b", "tinytable", x)
x <- gsub("styleCell_\\w+\\b", "tinytable", x)
x
}

x <- mtcars[1:4, 1:5]

tab <- tt(x, output = "html", theme = "striped")
expect_equal_to_reference(clean(tab), "_tinysnapshot/html-striped.rds")

tab <- tt(x, output = "html", theme = "striped") |>
style_tt(color = "orange")
expect_equal_to_reference(clean(tab), "_tinysnapshot/html-striped_orange.rds")

# tutorial.qmd: vectorized settings
tab <- tt(x, output = "html") |>
style_tt(
j = 2:3,
color = c("orange", "green"),
background = "black")
expect_equal_to_reference(clean(tab), "_tinysnapshot/html-vectorized_color_j.rds")

# Issue #58
tab <- tt(iris[1:10,], output = "html", align = "ccccc") |>
group_tt(j = list("Sepal" = 1:2, "Petal" = 3:4))
expect_equal_to_reference(clean(tab), "_tinysnapshot/html-issue58.rds")


# tutorial.qmd: heatmap
k <- data.frame(matrix(1:20, ncol = 5))
colnames(k) <- NULL
bg <- hcl.colors(20, "Inferno")
fg <- ifelse(as.matrix(k) < 17, tail(bg, 1), head(bg, 1))
fs <- 1:20
tab <- tt(k, output = "html", width = .5, theme = "void") |>
style_tt(
i = 1:4,
j = 1:5,
color = fg,
background = bg,
fontsize = fs)
expect_equal_to_reference(clean(tab), "_tinysnapshot/html-heatmap.rds")


0 comments on commit 822efd0

Please sign in to comment.