diff --git a/R/table.R b/R/table.R index 2113b2b677..d0b6768b15 100644 --- a/R/table.R +++ b/R/table.R @@ -27,6 +27,8 @@ #' expanded to a vector of individual letters, e.g. \code{'clc'} becomes #' \code{c('c', 'l', 'c')}, unless the output format is LaTeX. #' @param caption The table caption. +#' @param label The table reference label. By default, the label is obtained from +#' knitr:::opts_current. Only provide a label when using kable outside of RMarkdown. #' @param format.args A list of arguments to be passed to \code{\link{format}()} #' to format table values, e.g. \code{list(big.mark = ',')}. #' @param escape Boolean; whether to escape special characters when producing @@ -82,7 +84,7 @@ #' # can also set options(knitr.table.format = 'html') so that the output is HTML kable = function( x, format, digits = getOption('digits'), row.names = NA, col.names = NA, - align, caption = NULL, format.args = list(), escape = TRUE, ... + align, caption = NULL, label=NULL, format.args = list(), escape = TRUE, ... ) { # determine the table format @@ -104,8 +106,17 @@ kable = function( align = strsplit(align, '')[[1]] # create a label for bookdown if applicable + + #no label from markdown, no explicit label provided + if(is.null(knitr::opts_current$get('label')) & is.null(label)) label = 'unlabeled' + + #label from markdown, explicit labels will be ignored + if(!is.null(knitr::opts_current$get('label'))) label = knitr::opts_current$get('label') + + + #include label in caption if (!is.null(caption) && !is.na(caption)) caption = paste0( - create_label('tab:', opts_current$get('label'), latex = (format == 'latex')), + create_label('tab:', label, latex = (format == 'latex')), caption ) if (inherits(x, 'list')) { diff --git a/man/kable.Rd b/man/kable.Rd index 5d3375a0f8..b5ce7fc55e 100644 --- a/man/kable.Rd +++ b/man/kable.Rd @@ -5,7 +5,7 @@ \title{Create tables in LaTeX, HTML, Markdown and reStructuredText} \usage{ kable(x, format, digits = getOption("digits"), row.names = NA, col.names = NA, align, - caption = NULL, format.args = list(), escape = TRUE, ...) + caption = NULL, label = NULL, format.args = list(), escape = TRUE, ...) } \arguments{ \item{x}{An R object, typically a matrix or data frame.} @@ -35,6 +35,9 @@ default, row names are included if \code{rownames(x)} is neither \item{caption}{The table caption.} +\item{label}{The table reference label. By default, the label is obtained from +knitr:::opts_current. Only provide a label when using kable outside of RMarkdown.} + \item{format.args}{A list of arguments to be passed to \code{\link{format}()} to format table values, e.g. \code{list(big.mark = ',')}.}