Skip to content

Commit

Permalink
caption argument now supported for Markdown tables
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 24, 2024
1 parent 23383ee commit eac88b5
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
24 changes: 3 additions & 21 deletions R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ build_tt <- function(x, output = NULL) {

# finalize
out <- finalize_bootstrap(out)

# formal grid specification in pandoc includes lines everywhere
hlines <- getOption("tinytable_grid_hlines", default = TRUE)
if (isTRUE(hlines) && output == "markdown") {
out <- grid_hlines(out)
}
out <- finalize_grid(out)

m <- meta(x)
m$lazy_style <- list()
Expand All @@ -90,18 +85,5 @@ build_tt <- function(x, output = NULL) {
}


finalize_bootstrap <- function(x) {
if (meta(x)$output != "html") return(x)
out <- gsub(
"$tinytable_BOOTSTRAP_CLASS",
"table",
x,
fixed = TRUE)
# Rmarkdown and Quarto load their own bootstrap, which we probably don't want to override
if (isTRUE(getOption('knitr.in.progress'))) {
out <- strsplit(out, split = "\n")[[1]]
out <- out[!grepl("https://cdn.jsdelivr.net/npm/bootstrap", out, fixed = TRUE)]
out <- paste(out, collapse = "\n")
}
return(out)
}


16 changes: 16 additions & 0 deletions R/finalize_bootstrap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

finalize_bootstrap <- function(x) {
if (meta(x)$output != "html") return(x)
out <- gsub(
"$tinytable_BOOTSTRAP_CLASS",
"table",
x,
fixed = TRUE)
# Rmarkdown and Quarto load their own bootstrap, which we probably don't want to override
if (isTRUE(getOption('knitr.in.progress'))) {
out <- strsplit(out, split = "\n")[[1]]
out <- out[!grepl("https://cdn.jsdelivr.net/npm/bootstrap", out, fixed = TRUE)]
out <- paste(out, collapse = "\n")
}
return(out)
}
20 changes: 20 additions & 0 deletions R/finalize_grid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

finalize_grid <- function(x) {
if (meta(x)$output != "markdown") return(x)

out <- x

# formal grid specification in pandoc includes lines everywhere
# important for docx output
hlines <- getOption("tinytable_grid_hlines", default = TRUE)
if (isTRUE(hlines)) {
out <- grid_hlines(out)
}

cap <- meta(x, "caption")
if (is.character(cap) && length(cap) == 1) {
out <- paste0(out, "\n", "Table: ", cap, "\n")
}

return(out)
}
1 change: 1 addition & 0 deletions R/tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ tt <- function(x,
out <- meta(out, "nhead", if (is.null(colnames(x))) 0 else 1)
out <- meta(out, "nrows", nrow(x))
out <- meta(out, "ncols", ncol(x))
out <- meta(out, "caption", caption)
class(out) <- c("tinytable", class(out))

# build table
Expand Down
15 changes: 15 additions & 0 deletions inst/tinytest/_tinysnapshot/markdown-caption.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@



+------+-----+------+
| mpg | cyl | disp |
+======+=====+======+
| 21 | 6 | 160 |
+------+-----+------+
| 21 | 6 | 160 |
+------+-----+------+
| 22.8 | 4 | 108 |
+------+-----+------+

Table: Blah blah blah

9 changes: 7 additions & 2 deletions inst/tinytest/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ colnames(k) <- NULL
expect_snapshot_print(
tt(k), label = "markdown-nocolnames")


# group rows and columns
tab <- tt(mtcars[1:10, 1:5]) |>
group_tt(
i = list(
Expand All @@ -17,8 +17,13 @@ tab <- tt(mtcars[1:10, 1:5]) |>
j = list(
"Foo" = 2:3,
"Bar" = 4:5))

expect_snapshot_print(tab, label = "markdown-group_tt")

# simple caption
tab <- tt(mtcars[1:3, 1:3], caption = "Blah blah blah")
expect_snapshot_print(tab, label = "markdown-caption")




options(tinytable_print_output = NULL)

0 comments on commit eac88b5

Please sign in to comment.