R/toKable.R

Defines functions toKable getKnitrFormat

Documented in getKnitrFormat toKable

getKnitrFormat <- function(default = "latex") {
  options <- opts_knit$get(c("out.format", "rmarkdown.pandoc.to"))
  result <- default
  if (identical(options$out.format, "markdown"))
    result <- options$rmarkdown.pandoc.to
  else if (!is.null(options$out.format))
    result <- options$out.format
  if (result %in% c("html4", "html5")) result <- "html"
  result
}

toKable <- function(table, format = getKnitrFormat(), booktabs = TRUE, ...)
{ 
  if (!inherits(table, "tabular"))
    stop("'table' must be a 'tabular' object.")
	
  format <- match.arg(format, c("latex", "html", "markdown", "pandoc",
  			        "rst"))
  if (!(format %in% c("latex", "html")))
    stop("Only 'latex' and 'html' format are currently supported.")
  
  if (format == "latex") {
    save <- if (booktabs) booktabs() else table_options()
    lines <- paste0(capture.output(latex(table, ...)), collapse = "\n")
    table_options(save)
  } else
    lines <- paste0(capture.output(html(table, ...)), collapse = "\n")
  structure(lines,
	  format = format, class = "knitr_kable",
	  n_head = nrow(attr(table, "colLabels")))
}

Try the tables package in your browser

Any scripts or data that you put into this service are public.

tables documentation built on Nov. 12, 2018, 1:04 a.m.