From ce79e4059dce8c88d824f706b7d69c26673be388 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Tue, 19 Apr 2022 15:24:34 +0900 Subject: [PATCH] apply colformat_md on content rather than dataset (#72) Solves #61, #71 * apply colformat_md on content rather than dataset * update NEWS * update documents * build documents --- NEWS.md | 1 + R/colformat.R | 17 +- docs/articles/format_columns.html | 318 ++++++++++---------- docs/articles/group-rows.html | 424 +++++++++++++-------------- docs/articles/transform-headers.html | 202 ++++++------- docs/news/index.html | 3 + docs/pkgdown.yml | 2 +- docs/sitemap.xml | 63 ++++ 8 files changed, 555 insertions(+), 475 deletions(-) create mode 100644 docs/sitemap.xml diff --git a/NEWS.md b/NEWS.md index a0bef17..a1b9371 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # ftExtra 0.3.0.9999 * `as_paragraph_md` supports empty string, `""`, as an input (#68). +* `colformat_md` applies on actual content of flextable rather than input dataset (#72). * Improved support for the underline syntax in Pandoc's Markdown (#73). # ftExtra 0.3.0 diff --git a/R/colformat.R b/R/colformat.R index d96cf1e..3150c27 100644 --- a/R/colformat.R +++ b/R/colformat.R @@ -48,13 +48,15 @@ colformat_md <- function(x, } dataset <- x[[part]]$dataset - col <- names(tidyselect::eval_select(rlang::expr(c(!!.j)), dataset)) + content <- x[[part]][["content"]][["content"]][["data"]] + nm <- colnames(content) + col <- tidyselect::eval_select(rlang::expr(c(!!.j)), dataset[nm]) if (length(col) == 0) { return(x) } - texts <- unlist(dataset[col], use.names = FALSE) + texts <- purrr::map_chr(content[, col], paragraph2txt) # Must evaluate outside add_footnotes due to lazy evaluation of arguments ft <- flextable::compose(x, @@ -80,3 +82,14 @@ colformat_md <- function(x, where <- function(...) { tidyselect::vars_select_helpers$where(...) } + +paragraph2txt <- function(x) { + if (all(is.na(x$txt))) return(NA_character_) + + txt <- x[["txt"]] + img <- x[["img_data"]] + has_img <- !purrr::map_lgl(img, is.null) & !is.na(img) + txt[has_img] <- sprintf("![](%s)", img[has_img]) + + paste(txt[!is.na(txt)], collapse = "") +} diff --git a/docs/articles/format_columns.html b/docs/articles/format_columns.html index bc932f6..66857ab 100644 --- a/docs/articles/format_columns.html +++ b/docs/articles/format_columns.html @@ -126,7 +126,7 @@

Why markdown?"Al", as_sub("2"), "O", as_sub("3") ) ) -
+
 grouped_iris %>% as_flextable(hide_grouplabel = TRUE)
-
+