A minimal Markdown table generator using the pipe | as column separators.
Arguments
- x
A 2-dimensional object (e.g., a matrix or data frame).
- digits
The number of decimal places to be passed to
round(). It can be a integer vector of the same length as the number of columns inxto round columns separately. The default is3.- na
A character string to represent
NAvalues. The default is an empty string.- newline
A character string to substitute
\ninx(because pipe tables do not support line breaks in cells). The default is a space.- limit
The maximum number of rows to show in the table. If it is smaller than the number of rows, the data in the middle will be omitted. If it is of length 2, the second number will be used to limit the number of columns. Zero and negative values are ignored.
- escape
Whether to backslash-escape special Markdown characters (
\`, *, _, [, ], <, >, ~, \) in table cells. This prevents cell content from being interpreted as Markdown (e.g.,<foo:bar>being treated as a link). It can take a logical value (TRUEmeans all columns andFALSEmeans none), or a vector of column indices or names to escape selectively. The default isFALSE.
Details
The default argument values can be set via global options with the prefix
xfun.md_table., e.g., options(xfun.md_table.digits 2, xfun.md_table.na = 'n/a').
See also
knitr::kable() (which supports more features)
Examples
xfun::md_table(head(iris))
#> [1] "|Sepal.Length|Sepal.Width|Petal.Length|Petal.Width|Species|"
#> [2] "|--:|--:|--:|--:|---|"
#> [3] "|5.1|3.5|1.4|0.2|setosa|"
#> [4] "|4.9|3.0|1.4|0.2|setosa|"
#> [5] "|4.7|3.2|1.3|0.2|setosa|"
#> [6] "|4.6|3.1|1.5|0.2|setosa|"
#> [7] "|5.0|3.6|1.4|0.2|setosa|"
#> [8] "|5.4|3.9|1.7|0.4|setosa|"
xfun::md_table(mtcars, limit = c(10, 6))
#> [1] "| |mpg|cyl|...|gear|am|vs|"
#> [2] "|---|--:|--:|:-:|--:|--:|--:|"
#> [3] "|Mazda RX4|21.0|6|...|4|1|0|"
#> [4] "|Mazda RX4 Wag|21.0|6|...|4|1|0|"
#> [5] "|Datsun 710|22.8|4|...|4|1|1|"
#> [6] "|Hornet 4 Drive|21.4|6|...|3|0|1|"
#> [7] "|Hornet Sportabout|18.7|8|...|3|0|0|"
#> [8] "|⋮|⋮|⋮|...|⋮|⋮|⋮|"
#> [9] "|Lotus Europa|30.4|4|...|5|1|1|"
#> [10] "|Ford Pantera L|15.8|8|...|5|1|0|"
#> [11] "|Ferrari Dino|19.7|6|...|5|1|0|"
#> [12] "|Maserati Bora|15.0|8|...|5|1|0|"
#> [13] "|Volvo 142E|21.4|4|...|4|1|1|"
xfun::md_table(data.frame(x = "Hello <world>", y = 1), escape = TRUE)
#> [1] "|x|y|" "|---|--:|" "|Hello \\<world\\>|1|"
xfun::md_table(data.frame(x = "Hello <world>", y = 1), escape = "x")
#> [1] "|x|y|" "|---|--:|" "|Hello \\<world\\>|1|"