Skip to main content
The colformat_* functions apply display formatting to columns based on their data type. They all target the body part by default and use the values from get_flextable_defaults() for their default arguments.

colformat_double()

Format double (floating-point numeric) columns using formatC().
colformat_double(
  x,
  i = NULL,
  j = NULL,
  big.mark = get_flextable_defaults()$big.mark,
  decimal.mark = get_flextable_defaults()$decimal.mark,
  digits = get_flextable_defaults()$digits,
  na_str = get_flextable_defaults()$na_str,
  nan_str = get_flextable_defaults()$nan_str,
  prefix = "",
  suffix = ""
)
x
flextable
required
A flextable object.
i
integer | formula | logical
Row selector. NULL selects all rows.
j
integer | character | formula
Column selector. Only columns of type double are affected; other column types are silently skipped.
big.mark
string
Thousands separator. See formatC(). Defaults to the value in set_flextable_defaults().
decimal.mark
string
Decimal separator. See formatC(). Defaults to the value in set_flextable_defaults().
digits
integer
Number of digits after the decimal point. See formatC(). Defaults to the value in set_flextable_defaults().
na_str
string
String displayed for NA values.
nan_str
string
String displayed for NaN values.
prefix
string
default:""
String prepended to every formatted value.
suffix
string
default:""
String appended to every formatted value.

Example

ft <- flextable(head(mtcars))
ft <- colformat_double(x = ft, big.mark = ",", digits = 2, na_str = "N/A")
autofit(ft)

colformat_int()

Format integer columns.
colformat_int(
  x,
  i = NULL,
  j = NULL,
  big.mark = get_flextable_defaults()$big.mark,
  na_str = get_flextable_defaults()$na_str,
  nan_str = get_flextable_defaults()$nan_str,
  prefix = "",
  suffix = ""
)
big.mark
string
Thousands separator. See format().

Example

ft <- flextable(head(mtcars))
ft <- colformat_int(x = ft, j = c("vs", "am", "gear", "carb"), prefix = "# ")
ft

colformat_num()

Format numeric columns using R’s format() function, matching typical R console output. Scientific notation is disabled and NA values are replaced.
colformat_num(
  x,
  i = NULL,
  j = NULL,
  big.mark = get_flextable_defaults()$big.mark,
  decimal.mark = get_flextable_defaults()$decimal.mark,
  na_str = get_flextable_defaults()$na_str,
  nan_str = get_flextable_defaults()$nan_str,
  prefix = "",
  suffix = "",
  ...
)
...
Additional arguments passed to format(). Note: scientific and digits cannot be used.

Example

dat <- mtcars
dat[2, 1] <- NA
ft <- flextable(head(dat))
ft <- colformat_num(x = ft, big.mark = " ", decimal.mark = ",", na_str = "N/A")
ft <- autofit(ft)
ft

colformat_char()

Format character and factor columns.
colformat_char(
  x,
  i = NULL,
  j = NULL,
  na_str = get_flextable_defaults()$na_str,
  nan_str = get_flextable_defaults()$nan_str,
  prefix = "",
  suffix = ""
)

Example

ft <- flextable(head(iris))
ft <- colformat_char(x = ft, j = "Species", suffix = "!")
ft <- autofit(ft)
ft

colformat_lgl()

Format logical columns.
colformat_lgl(
  x,
  i = NULL,
  j = NULL,
  true = "true",
  false = "false",
  na_str = get_flextable_defaults()$na_str,
  nan_str = get_flextable_defaults()$nan_str,
  prefix = "",
  suffix = ""
)
true
string
default:"true"
String displayed for TRUE values.
false
string
default:"false"
String displayed for FALSE values.

Example

dat <- data.frame(a = c(TRUE, FALSE), b = c(FALSE, TRUE))
ft <- flextable(dat)
ft <- colformat_lgl(x = ft, j = c("a", "b"))
autofit(ft)

colformat_date()

Format Date columns.
colformat_date(
  x,
  i = NULL,
  j = NULL,
  fmt_date = get_flextable_defaults()$fmt_date,
  na_str = get_flextable_defaults()$na_str,
  nan_str = get_flextable_defaults()$nan_str,
  prefix = "",
  suffix = ""
)
fmt_date
string
Date format string passed to strptime(). Defaults to "%Y-%m-%d" unless overridden with set_flextable_defaults().

Example

dat <- data.frame(z = Sys.Date() + 1:3, w = Sys.Date() - 1:3)
ft <- flextable(dat)
ft <- colformat_date(x = ft)
ft <- autofit(ft)
ft

colformat_datetime()

Format POSIXct / POSIXlt (datetime) columns.
colformat_datetime(
  x,
  i = NULL,
  j = NULL,
  fmt_datetime = get_flextable_defaults()$fmt_datetime,
  na_str = get_flextable_defaults()$na_str,
  nan_str = get_flextable_defaults()$nan_str,
  prefix = "",
  suffix = ""
)
fmt_datetime
string
Datetime format string passed to strptime(). Defaults to "%Y-%m-%d %H:%M:%S" unless overridden with set_flextable_defaults().

Example

dat <- data.frame(z = Sys.time() + (1:3) * 24, w = Sys.Date() - (1:3) * 24)
ft <- flextable(dat)
ft <- colformat_datetime(x = ft)
ft <- autofit(ft)
ft

colformat_image()

Render character columns that contain image file paths as inline images.
colformat_image(
  x,
  i = NULL,
  j = NULL,
  width,
  height,
  na_str = get_flextable_defaults()$na_str,
  nan_str = get_flextable_defaults()$nan_str,
  prefix = "",
  suffix = ""
)
width
numeric
required
Image display width in inches.
height
numeric
required
Image display height in inches.

Example

img.file <- file.path(R.home("doc"), "html", "logo.jpg")

dat <- head(iris)
dat$Species <- as.character(dat$Species)
dat[c(1, 3, 5), "Species"] <- img.file

ft <- flextable(dat)
ft <- colformat_image(ft, i = c(1, 3, 5), j = "Species", width = .20, height = .15)
ft <- autofit(ft)
ft

set_formatter()

Apply custom formatter functions to specified columns. Each formatter receives a vector of column values and must return a character vector.
set_formatter(x, ..., values = NULL, part = "body")
x
flextable
required
A flextable object.
...
name-value pairs
Named arguments where names are column keys and values are single-argument formatter functions.
values
list | function
A named list of formatter functions (names are column keys), or a single function applied to every column. If supplied, ... is ignored.
part
string
default:"body"
Which part to apply formatting to: "body", "header", or "footer". The value "all" is not allowed.

Example

ft <- flextable(head(iris))
ft <- set_formatter(
  x = ft,
  Sepal.Length = function(x) sprintf("%.02f", x),
  Sepal.Width  = function(x) sprintf("%.04f", x)
)
ft <- theme_vanilla(ft)
ft

Return value

All functions return the modified flextable object.

See also