Skip to contents

Convenience wrapper around knitr::kable(format = "pipe") to create a Markdown pipe table.

Usage

pipe_table(
  x,
  incl_rownames = NULL,
  strong_colnames = TRUE,
  strong_rownames = TRUE,
  align = NULL,
  label = NULL,
  digits = getOption("digits"),
  format_args = list()
)

Arguments

x

Dataframe/tibble/matrix to be converted to a pipe table.

incl_rownames

Whether to include row names or not. A logical scalar or NULL. If NULL, row names are included if rownames(x) is neither NULL nor identical to seq_len(nrow(x)).

strong_colnames

Whether or not to highlight column names by formatting them <strong> (wrapping them in two asterisks).

strong_rownames

Whether or not to highlight row names by formatting them <strong> (wrapping them in two asterisks).

align

Column alignment. Either NULL for auto-alignment or a character vector consisting of 'l' (left), 'c' (center) and/or 'r' (right). If align = NULL, numeric columns are right-aligned, and other columns are left-aligned. If length(align) == 1L, the string will be expanded to a vector of individual letters, e.g. 'clc' becomes c('c', 'l', 'c').

label

The table reference label. By default, the label is obtained from knitr::opts_current$get('label') (i.e., the current chunk label). To disable the label, use label = NA.

digits

Maximum number of digits for numeric columns, passed to round(). This can also be a vector of length ncol(x), to set the number of digits for individual columns.

format_args

A list of arguments to be passed to base::format() to format table values, e.g. list(big.mark = ',').

Value

A character vector.

Details

Missing values (NA) in the table are displayed as NA by default. If you want to display them with other characters, you can set the option knitr.kable.NA, e.g. options(knitr.kable.NA = '') to hide NA values.

You can set the option knitr.kable.max_rows to limit the number of rows to show in the table, e.g., options(knitr.kable.max_rows = 30).

Create tables dynamically in roxygen2 documentation

This function can be useful to create tables inside roxygen2 documentation programmatically from data using dynamic R code.

For example, the inline code

`r mtcars |> head() |> pipe_table()`

should produce the following table in roxygen2 7.1.0 and above:

mpgcyldisphpdratwtqsecvsamgearcarb
Mazda RX421.061601103.902.62016.460144
Mazda RX4 Wag21.061601103.902.87517.020144
Datsun 71022.84108933.852.32018.611141
Hornet 4 Drive21.462581103.083.21519.441031
Hornet Sportabout18.783601753.153.44017.020032
Valiant18.162251052.763.46020.221031

See also

xfun::md_table()

Other (Pandoc) Markdown functions: as_md_list(), as_md_val_list(), as_md_vals(), md_verb(), strip_md(), strip_md_footnotes()

Examples

mtcars |> head() |> pal::pipe_table() |> pal::cat_lines()
#> |                      | **mpg**| **cyl**| **disp**| **hp**| **drat**| **wt**| **qsec**| **vs**| **am**| **gear**| **carb**|
#> |:---------------------|-------:|-------:|--------:|------:|--------:|------:|--------:|------:|------:|--------:|--------:|
#> |**Mazda RX4**         |    21.0|       6|      160|    110|     3.90|  2.620|    16.46|      0|      1|        4|        4|
#> |**Mazda RX4 Wag**     |    21.0|       6|      160|    110|     3.90|  2.875|    17.02|      0|      1|        4|        4|
#> |**Datsun 710**        |    22.8|       4|      108|     93|     3.85|  2.320|    18.61|      1|      1|        4|        1|
#> |**Hornet 4 Drive**    |    21.4|       6|      258|    110|     3.08|  3.215|    19.44|      1|      0|        3|        1|
#> |**Hornet Sportabout** |    18.7|       8|      360|    175|     3.15|  3.440|    17.02|      0|      0|        3|        2|
#> |**Valiant**           |    18.1|       6|      225|    105|     2.76|  3.460|    20.22|      1|      0|        3|        1|