Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

highlight

Highlight and link a code block


Description

This function:

  • syntax highlights code

  • links function calls to their documentation (where possible)

  • in comments, translates ANSI escapes in to HTML equivalents.

Usage

highlight(text, classes = classes_chroma(), pre_class = NULL, code = FALSE)

classes_pandoc()

classes_chroma()

Arguments

text

String of code to highlight and link.

classes

A mapping between token names and CSS class names. Bundled classes_pandoc() and classes_chroma() provide mappings that (roughly) match Pandoc and chroma (used by hugo) classes so you can use existing themes.

pre_class

Class(es) to give output <pre>.

code

If TRUE, wrap output in a <code> block

Value

If text is valid R code, an HTML <pre> tag. Otherwise, NA.

A string containing syntax highlighted HTML or NA (if text isn't parseable).

Examples

cat(highlight("1 + 1"))
cat(highlight("base::t(1:3)"))

# Unparseable R code returns NA
cat(highlight("base::t("))

downlit

Syntax Highlighting and Automatic Linking

v0.2.1
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.