Skip to content

Commit

Permalink
use options(knitr.kable.max_rows) for kable() to control the maximum …
Browse files Browse the repository at this point in the history
…number of rows to be generated in the table
  • Loading branch information
yihui committed Apr 10, 2024
1 parent b2d4064 commit aa11db8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.46.1
Version: 1.46.2
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGES IN knitr VERSION 1.47

## NEW FEATURES

- For `kable()`, you can set the global option `knitr.kable.max_rows` to limit the number of rows to show in the table, e.g., `options(knitr.kable.max_rows = 30)`. This is a way to prevent `kable()` from generating a huge table from a large data object by accident.

# CHANGES IN knitr VERSION 1.46

Expand Down
5 changes: 5 additions & 0 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#' default. If you want to display them with other characters, you can set the
#' option \code{knitr.kable.NA}, e.g. \code{options(knitr.kable.NA = '')} to
#' hide \code{NA} values.
#'
#' You can set the option \code{knitr.kable.max_rows} to limit the number of
#' rows to show in the table, e.g., \code{options(knitr.kable.max_rows = 30)}.
#' @param x For \code{kable()}, \code{x} is an R object, which is typically a
#' matrix or data frame. For \code{kables()}, a list with each element being a
#' returned value from \code{kable()}.
Expand Down Expand Up @@ -123,6 +126,8 @@ kable = function(
caption = kable_caption(label, caption, format)

if (!is.matrix(x)) x = as.data.frame(x)
# show the maximum number of rows if set
if (is.numeric(nr <- getOption('knitr.kable.max_rows'))) x = head(x, nr)
if (identical(col.names, NA)) col.names = colnames(x)
m = ncol(x)
# numeric columns
Expand Down
3 changes: 3 additions & 0 deletions man/kable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aa11db8

Please sign in to comment.