Skip to content

Commit

Permalink
markdown bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 14, 2024
1 parent a41dd80 commit 4c1f03c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
29 changes: 21 additions & 8 deletions R/tt_markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ align_str_center <- function(x, pad_n = NULL) {
tt_markdown <- function(tab, caption, ...) {

# fake spans
colnames(tab) <- gsub("\\|{4}", " / ", colnames(tab))
if (!is.null(colnames(tab))) {
colnames(tab) <- gsub("\\|{4}", " / ", colnames(tab))
}

# align content
# this is an infrastructure already. Can be used when
align <- rep("l", ncol(tab))
for (i in seq_along(tab)) {
pad_n <- max(nchar(colnames(tab)[i]), max(nchar(tab[[i]])))

# otherwise we can't take nchar()
tab[[i]] <- as.character(tab[[i]])

pad_n <- 0
if (!is.null(colnames)) pad_n <- max(pad_n, nchar(colnames(tab)[i]))
pad_n <- max(pad_n, max(nchar(as.character(tab[[i]]))))
if (align[[i]] == "l") {
tab[[i]] <- align_str_left(tab[[i]], pad_n = pad_n)
} else if (align[[i]] == "r") {
Expand All @@ -38,12 +46,14 @@ tt_markdown <- function(tab, caption, ...) {
}

# bind centered column names
header <- as.data.frame(as.list(colnames(tab)))
colnames(header) <- colnames(tab)
for (i in seq_along(tab)) {
header[[i]] <- align_str_center(header[[i]], nchar(tab[[i]][1]))
if (!is.null(colnames(tab))) {
header <- as.data.frame(as.list(colnames(tab)))
colnames(header) <- colnames(tab)
for (i in seq_along(tab)) {
header[[i]] <- align_str_center(header[[i]], nchar(tab[[i]][1]))
}
tab <- rbind(header, tab)
}
tab <- rbind(header, tab)

# pipes
tab[[1]] <- paste("|", tab[[1]])
Expand All @@ -60,7 +70,10 @@ tt_markdown <- function(tab, caption, ...) {
ruler <- sub("\\|-", "|:", ruler) # only first
ruler <- gsub("-$", "", ruler) # only first

hrule <- 1
hrule <- NULL
if (!is.null(colnames(tab))) {
hrule <- 1
}
for (h in hrule) {
tab <- append(tab, ruler, after = h)
}
Expand Down
11 changes: 11 additions & 0 deletions inst/tinytest/_tinysnapshot/markdown-nocolnames.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5 | 3.6 | 1.4 | 0.2 | setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | setosa |
| 4.6 | 3.4 | 1.4 | 0.3 | setosa |
| 5 | 3.4 | 1.5 | 0.2 | setosa |
| 4.4 | 2.9 | 1.4 | 0.2 | setosa |
| 4.9 | 3.1 | 1.5 | 0.1 | setosa |
20 changes: 20 additions & 0 deletions inst/tinytest/helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
options("tinysnapshot_device" = "svglite")
options("tinysnapshot_tol" = 200)

if (isTRUE(insight::check_if_installed("cmdstanr", quietly = TRUE))) {
options("brms.backend" = "cmdstanr")
}

# libraries
requiet <- function(package) {
void <- capture.output(
pkg_available <- tryCatch(suppressPackageStartupMessages(suppressWarnings(suppressMessages(tryCatch(
isTRUE(require(package, warn.conflicts = FALSE, character.only = TRUE)),
error = function(e) FALSE
))))))
return(pkg_available)
}

requiet("tinytest")
requiet("tinysnapshot")

8 changes: 8 additions & 0 deletions inst/tinytest/test-markdown.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source("helpers.R")
using("tinysnapshot")


pkgload::load_all()
k = iris[1:10,]
colnames(k) <- NULL
expect_snapshot_print(tt(k), "markdown-nocolnames")

0 comments on commit 4c1f03c

Please sign in to comment.