diff --git a/DESCRIPTION b/DESCRIPTION index 5a35f17d..ab44cc89 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,6 +9,7 @@ Enhances: knitr Suggests: altdoc, + pandoc, rmarkdown, rstudioapi, tinysnapshot, diff --git a/R/save_tt.R b/R/save_tt.R index 49188114..0b40f7c6 100644 --- a/R/save_tt.R +++ b/R/save_tt.R @@ -38,6 +38,7 @@ save_tt <- function(x, output, overwrite = FALSE) { return(as.character(out)) } + file_ext <- tools::file_ext(output) output_format <- switch(file_ext, @@ -49,6 +50,7 @@ save_tt <- function(x, output, overwrite = FALSE) { "Rmd" = "markdown", "qmd" = "markdown", "txt" = "markdown", + "docx" = "markdown", stop("The supported file extensions are: .png, .html, .pdf, .tex, and .md.", call. = FALSE)) # evaluate styles at the very end of the pipeline, just before writing @@ -88,12 +90,37 @@ save_tt <- function(x, output, overwrite = FALSE) { \\begin{document} %s \\end{document}", - tmp) + tmp) d <- tempdir() f <- file.path(d, "index.tex") write(tmp, f) tinytex::xelatex(f, pdf_file = output) + + } else if (file_ext == "docx") { + assert_dependency("pandoc") + tmp <- markdown_hlines(x) + fn <- file.path(tempdir(), "temp.md") + writeLines(x, fn) + writeLines(x, "~/Downloads/trash.md") + pandoc::pandoc_convert(file = fn, to = "docx", output = output) + } + + return(invisible(TRUE)) + } -return(invisible(TRUE)) - } + +# insert horizontal rules everywhere (important for word) +markdown_hlines <- function(x) { + rule_line <- grid_line(meta(x, "col_widths"), "-") + lines <- strsplit(x, split = "\\n")[[1]] + if (length(lines) > 1) { + for (idlines in length(lines):2) { + if (!startsWith(lines[idlines - 1], "+") && !startsWith(lines[idlines], "+")) { + lines <- c(lines[1:(idlines - 1)], rule_line, lines[idlines:length(lines)]) + } + } + } + out <- paste(lines, collapse = "\n") + return(out) +} diff --git a/R/tt_grid.R b/R/tt_grid.R index f9398b4c..4bd452f3 100755 --- a/R/tt_grid.R +++ b/R/tt_grid.R @@ -100,7 +100,7 @@ group_grid <- function(x, i = NULL, j = NULL, ...) { group_grid_col <- function(x, j, ...) { - + m <- meta(x) # columns header <- empty_cells(j) cw <- meta(x, "col_widths") @@ -114,6 +114,8 @@ group_grid_col <- function(x, j, ...) { x <- x[!x %in% c("\\n", "")] out <- out[1:(length(out) - 1)] out <- paste(c(out, x), collapse = "\n") + attr(out, "tinytable_meta") <- m + class(out) <- class(x) return(out) } @@ -125,7 +127,7 @@ group_grid_row <- function(x, i, ...) { # header body_min <- utils::head(grep("^\\+==", out), 1) + 1 # no header - if (is.na(body_min)) { + if (is.na(body_min) || length(body_min) == 0) { body_min <- utils::head(grep("^\\+--", out), 1) + 1 } body_max <- utils::tail(grep("^\\+--", out), 1) - 1 @@ -137,7 +139,7 @@ group_grid_row <- function(x, i, ...) { cw <- meta(x, "col_widths") cw <- sum(cw) + length(cw) - 1 for (idx in rev(seq_along(i))) { - tmp = trimws(as.character(tt_grid(matrix(names(i)[idx]), col_widths = cw))) + tmp <- trimws(as.character(tt_grid(matrix(names(i)[idx]), col_widths = cw))) mid <- c(mid[1:(i[idx] - 1)], tmp, mid[i[idx]:length(body)]) } @@ -146,7 +148,6 @@ group_grid_row <- function(x, i, ...) { attr(out, "tinytable_meta") <- meta(x) class(out) <- class(x) - return(out) }