klippy insert copy to clipboard buttons (or "klippies") in R Markdown HTML documents. In the rendered document, "klippies" are inserted in the upper left corner of the code chunks. klippy() function is suited for a call in a knitr code chunk.

klippy(
  lang = c("r", "markdown"),
  all_precode = FALSE,
  position = c("top", "left"),
  color = "auto",
  tooltip_message = "Copy code",
  tooltip_success = "Copied!"
)

Arguments

lang

A character string or a vector of character strings with language names. If a character string contains multiple languages names, these names have to be separated by boundaries (e.g., spaces). Void string can be passed to lang argument.

all_precode

A logical scalar. If TRUE, a "klippy" is added to all HTML <pre> elements having an HTML <code> element as a child.

position

A character vector with klippy position. Accepted values are "top", "bottom", "left" and "right". Abbreviated forms are allowed.

color

String of any of the three kinds of R color specifications, i.e., either a color name (as listed by colors()), a hexadecimal string of the form "#rrggbb" or "#rrggbbaa" (see rgb()), or a positive integer i meaning palette()[i]. Default value is "auto": color is set to the anchor color of the document.

tooltip_message

String with the tooltip message.

tooltip_success

String with the tooltip message shown when code is successfully copied.

Value

An HTML tag object that can be rendered as HTML using as.character().

Details

klippy() function appends JavaScript functions and CSS in the rendered document that:

  1. Add klippy to the class attribute of selected <pre> elements.

  2. Add a <button> element (a "klippy") as a child for all <pre> elements with a klippy class attribute.

  3. Instantiate clipboard.js event listeners and attach them to klippies.

klippy class can also be appended to a <pre> element using knitr class.source chunk option. "Klippy" buttons are not rendered if the browser does not support clipboard.js library (see here for details).

Examples

tf <- tempfile(fileext = c(".Rmd", ".html")) writeLines( c("```{r klippy, echo=FALSE, include=TRUE}", "klippy::klippy()", "```", "Insert this chunk in your `Rmd` file:", "````markdown", "`r ''````{r klippy, echo=FALSE, include=TRUE}", "klippy::klippy()", "```", "````" ), tf[1] ) if (FALSE) { rmarkdown::render(tf[1], "html_document", tf[2]) browseURL(paste0("file://", tf[2]))}