Format columns as markdown

This vignette introduces how to format columns in flextable.

library(ftExtra)

Basic examples

The example below shows that colformat_md() function parses markdown texts in the flextable object.

data.frame(
  a = c("**bold**", "*italic*"),
  b = c("^superscript^", "~subscript~"),
  c = c("`code`", "[underline]{.underline}"),
  d = c("*[**~ft~^Extra^**](https://ftextra.atusy.net/) is*",
        "[Cool]{.underline shading.color='skyblue'}"),
  stringsAsFactors = FALSE
) %>%
  as_flextable() %>%
  colformat_md()

a

b

c

d

bold

superscript

code

ftExtra is

italic

subscript

underline

Cool

The table header can also be formatted by specifying part = "header" or "all" to colformat_md()

Supported syntax are

Note that other syntax may result in unexpected behaviors. The ftExtra package does not support multiple paragraphs or block elements other than the paragraph.

Footnotes

Currently footnotes have some limitations.

For example,

data.frame(
  package = "ftExtra",
  description = "Extensions for 'Flextable'^[Supports of footnotes]",
  stringsAsFactors = FALSE
) %>%
  as_flextable() %>%
  colformat_md() %>%
  flextable::autofit(add_w = 0.5)

package

description

ftExtra

Extensions for Flextable1

1Supports of footnotes

Reference symboles can be configured by footnote_options(). Of course, markdown can be used inside footnotes as well.

data.frame(
  package = "ftExtra^[Short of *flextable extra*]",
  description = "Extensions for 'Flextable'^[Supports of footnotes]"
) %>%
  as_flextable() %>%
  colformat_md(
    .footnote_options = footnote_options(ref = "i",
                                         prefix = '[',
                                         suffix = ']',
                                         start = 2,
                                         inline = TRUE,
                                         sep = "; ")
  ) %>%
  flextable::autofit(add_w = 0.5)

package

description

ftExtra[ii]

Extensions for Flextable[iii]

[ii]Short of flextable extra; [iii]Supports of footnotes

Images

Images can be inserted optionally with width and/or height attributes. Specifying one of them changes the other while keeping the aspect ratio.

data.frame(
  R = c("![](https://www.r-project.org/logo/Rlogo.png){width=.5} is the great language"),
  stringsAsFactors = FALSE
) %>%
  as_flextable() %>%
  colformat_md() %>%
  flextable::autofit()

R

is the great language

The R logo is distributed by The R Foundation with the CC-BY-SA 4.0 license.

Line breaks

By default, soft line breaks becomes spaces.

data.frame(linebreak = c("a\nb"), stringsAsFactors = FALSE) %>%
  as_flextable() %>%
  colformat_md()

linebreak

a b

Pandoc’s markdown supports hard line breaks by adding a backslash or double spaces at the end of a line.

data.frame(linebreak = c("a\\\nb"), stringsAsFactors = FALSE) %>%
  as_flextable() %>%
  colformat_md()

linebreak

a
b

It is also possible to make \n as a hard line break by extending Pandoc’s Markdown.

data.frame(linebreak = c("a\nb"), stringsAsFactors = FALSE) %>%
  as_flextable() %>%
  colformat_md(md_extensions = "+hard_line_breaks")

linebreak

a
b

Citations

Citations is experimentally supported. Note that there are no citation lists. It is expected to be produced by using R Markdown.

First, create a ftExtra.bib file like below.

#> @Manual{R-ftExtra,
#>   title = {ftExtra: Extensions for Flextable},
#>   author = {Atsushi Yasumoto},
#>   note = {https://ftextra.atusy.net, https://github.com/atusy/ftExtra},
#>   year = {2020},
#> }

Second, specify it within the YAML front matter. It may also be required to cite references in the nocite field.

---
bibliography: ftExtra.bib
nocite: @R-ftExtra
---

Finally, cite the references within tables.

data.frame(
  Cite = c("@R-ftExtra", "[@R-ftExtra]", "[-@R-ftExtra]"),
  stringsAsFactors = FALSE
) %>%
  as_flextable() %>%
  colformat_md() %>%
  flextable::autofit(add_w = 0.2)

Cite

Yasumoto (2020)

(Yasumoto 2020)

(2020)

Math

The rendering of math is also possible.

data.frame(math = "$e^{i\\theta} = \\cos \\theta + i \\sin \\theta$",
           stringsAsFactors = FALSE) %>%
  as_flextable() %>%
  colformat_md() %>%
  flextable::autofit(add_w = 0.2)

math

eiθ = cos θ + isin θ

Note that results can be insufficient. This feature relies on Pandoc’s HTML writer, which

render TeX math as far as possible using Unicode characters
https://pandoc.org/MANUAL.html#math-rendering-in-html

Emoji

Pandoc’s markdown provides an extension, emoji. To use it with colformat_md(), specify md_extensions="+emoji".

data.frame(emoji = c(":+1:"), stringsAsFactors = FALSE) %>%
  as_flextable() %>%
  colformat_md(md_extensions = "+emoji")

emoji

👍