Skip to contents

This function is a wrapper around save_kable from the kableExtra package that allows you to write a kable object as part of a knitr document as an output for later use. It is designed to be used in a code block. The file name, if not specified, will be the label of the code block.

Usage

write_kbl(
  kbl_obj,
  file = NULL,
  target_dir = NULL,
  device = "pdf",
  bs_theme = "bootstrap",
  ...
)

Arguments

kbl_obj

The knitr_kable object to be written.

file

The name of the file to be written. If not specified, the name will be based on the current knitr code block label.

target_dir

The directory where the file will be written. If not specified, the current working directory will be used.

device

The device to be used for saving the file. Options include "pdf" (default), "html", "latex", "png", and "jpeg". Note that a Chromium-based browser (e.g., Google Chrome, Chromium, Microsoft Edge or Brave) is required on your system for all options except "latex'. If a suitable browser is not available, the function will stop and return an error message.

bs_theme

The Bootstrap theme to be applied to the kable object (only applicable for HTML output). Default is "bootstrap".

...

Additional arguments to be passed to the save_kable function from the kableExtra package.

Value

The path of the written file.

Examples

if (FALSE) { # \dontrun{
library(knitr)

table_dir <- file.path(tempdir(), "table")

mtcars_kbl <- kable(
 x = mtcars[1:5, ],
 format = "html")

# Write a kable object as a PDF file
write_kbl(
 kbl_obj = mtcars_kbl,
 file = "kable_pdf",
 target_dir = table_dir,
 device = "pdf")

# Write a kable as an HTML file with a custom Bootstrap theme
write_kbl(
 kbl_obj = mtcars_kbl,
 file = "kable_html",
 target_dir = table_dir,
 device = "html",
 bs_theme = "flatly")

unlink(table_dir)
} # }