Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/davidgohel/flextable/llms.txt

Use this file to discover all available pages before exploring further.

Chunks are the smallest content units inside a paragraph. Use these functions inside as_paragraph() to compose rich cell content.

as_chunk()

Add formatted text to a paragraph.
as_chunk(x, props = NULL, formatter = format_fun, ...)
x
any
required
Text or any value that can be formatted as text using the formatter function.
props
fp_text
An fp_text_default() or officer::fp_text() object specifying font properties. If NULL, the cell default is used.
formatter
function
default:"format_fun"
A function that converts x to a character vector.
...
Additional arguments passed to formatter.

Example

library(officer)
ft <- flextable(head(iris))
ft <- compose(
  ft,
  j = "Sepal.Length",
  value = as_paragraph(
    "Sepal.Length value is ",
    as_chunk(Sepal.Length, props = fp_text(color = "red"))
  ),
  part = "body"
)
ft <- autofit(ft)
ft

as_b()

Produce a bold text chunk.
as_b(x)
x
chunk | any
required
A chunk object or a value to convert to a chunk. If not already a chunk, it is converted via as_chunk().

as_i()

Produce an italic text chunk.
as_i(x)
x
chunk | any
required
A chunk object or a value converted to a chunk.

as_sub()

Produce a subscript text chunk.
as_sub(x)
x
chunk | any
required
A chunk object or a value converted to a chunk.

as_sup()

Produce a superscript text chunk.
as_sup(x)
x
chunk | any
required
A chunk object or a value converted to a chunk.

as_bracket()

Paste values together and wrap the result in brackets.
as_bracket(..., sep = ", ", p = "(", s = ")")
...
any
required
Values to paste together. Each is formatted with formatC().
sep
string
default:", "
Separator between values.
p
string
default:"("
Prefix character.
s
string
default:")"
Suffix character.

Example

ft <- flextable(head(iris), col_keys = c("Species", "Sepal", "Petal"))
ft <- compose(
  ft,
  j = "Sepal",
  value = as_paragraph(as_bracket(Sepal.Length, Sepal.Width))
)
ft

colorize()

Apply a text color to a chunk.
colorize(x, color)
x
chunk | any
required
A chunk object or a value converted to a chunk.
color
string
required
A valid color string (e.g., "red", "#FF0000").

Example

ft <- flextable(head(iris), col_keys = c("Sepal.Length", "dummy"))
ft <- compose(
  ft,
  j = "dummy",
  value = as_paragraph(colorize(Sepal.Length, color = "red"))
)
ft

as_highlight()

Apply a background highlight color to a chunk.
as_highlight(x, color)
x
chunk | any
required
A chunk object or a value converted to a chunk.
color
string
required
A valid color string used as the highlight (background) color.

Example

ft <- flextable(head(iris), col_keys = c("Sepal.Length", "dummy"))
ft <- compose(
  ft,
  j = "dummy",
  value = as_paragraph(as_highlight(Sepal.Length, color = "yellow"))
)
ft

Create a clickable hyperlink chunk.
hyperlink_text(x, props = NULL, formatter = format_fun, url, ...)
x
any
required
The link display text.
props
fp_text
Font properties from fp_text_default() or officer::fp_text().
formatter
function
default:"format_fun"
A function that converts x to a character vector.
url
string
required
The URL. URLs are not encoded; they are preserved as-is.
Requires the officedown package in R Markdown contexts with Word output.

Example

dat <- data.frame(
  col  = "Google it",
  href = "https://www.google.fr/search?source=hp&q=flextable+R+package",
  stringsAsFactors = FALSE
)
ft <- flextable(dat)
ft <- compose(
  x = ft,
  j = "col",
  value = as_paragraph(
    "This is a link: ",
    hyperlink_text(x = col, url = href)
  )
)
ft

as_equation()

Insert a MathJax equation chunk. Requires the equatags package.
as_equation(x, width = 1, height = .2, unit = "in", props = NULL)
x
character
required
MathJax equation strings.
width
numeric
default:"1"
Display width of the equation.
height
numeric
default:"0.2"
Display height of the equation.
unit
string
default:"in"
Unit for width and height: "in", "cm", or "mm".
props
fp_text
Font properties applied to the equation text.

as_image()

Insert an image chunk from a file path.
as_image(src, width = NULL, height = NULL, unit = "in", guess_size = TRUE, ...)
src
character
required
Path to the image file.
width
numeric
Display width. Can be omitted when guess_size = TRUE and the magick package is installed.
height
numeric
Display height. Can be omitted when guess_size = TRUE and magick is installed.
unit
string
default:"in"
Unit for width and height: "in", "cm", or "mm".
guess_size
logical
default:"TRUE"
If TRUE and magick is installed, image dimensions are read from the file automatically.
PowerPoint cannot mix images and text in a paragraph. Images are removed when outputting to PowerPoint format.

Example

img.file <- file.path(R.home("doc"), "html", "logo.jpg")
if (require("magick")) {
  ft <- flextable(head(iris))
  ft <- compose(
    ft,
    i = 1:3, j = 1,
    value = as_paragraph(
      as_image(src = img.file),
      " ",
      as_chunk(Sepal.Length, props = fp_text_default(color = "red"))
    ),
    part = "body"
  )
  ft <- autofit(ft)
  ft
}

See also

Build docs developers (and LLMs) love