Title: | Syntax Highlighting for R Source Code |
---|---|
Description: | Provides syntax highlighting for R source code. Currently it supports LaTeX and HTML output. Source code of other languages is supported via Andre Simon's highlight package (<https://gitlab.com/saalen/highlight>). |
Authors: | Yihui Xie [aut, cre] |
Maintainer: | Yihui Xie <[email protected]> |
License: | GPL |
Version: | 0.11.1 |
Built: | 2024-08-23 05:47:36 UTC |
Source: | https://github.com/yihui/highr |
This function calls Highlight to syntax highlight a code fragment.
hi_andre(code, language, format = "html")
hi_andre(code, language, format = "html")
code |
a character string of the source code |
language |
the input language (c, cpp, python, r, ...); see
|
format |
the output format (html, latex, ...) |
A character string for the syntax highlighted code.
Andre Simon's Highlight package https://gitlab.com/saalen/highlight.
## Not run: hi_andre("1+1", language = "R") hi_andre("void main() {\nreturn(0)\n}", language = "c", format = "latex") ## End(Not run)
## Not run: hi_andre("1+1", language = "R") hi_andre("void main() {\nreturn(0)\n}", language = "c", format = "latex") ## End(Not run)
This function parse
s the R code, fetches the tokens in it
(getParseData
), and attach syntax highlighting commands onto
them. With proper style definitions for these commands (such as colors or
font styles), the R code will be syntax highlighted in the LaTeX/HTML output.
The two functions hi_latex
and hi_html
are wrappers of
hilight
for LaTeX and HTML output, respectively.
hilight( code, format = c("latex", "html"), markup, prompt = FALSE, fallback = FALSE ) hi_latex(code, ...) hi_html(code, ...)
hilight( code, format = c("latex", "html"), markup, prompt = FALSE, fallback = FALSE ) hi_latex(code, ...) hi_html(code, ...)
code |
a character string (the R source code) |
format |
the output format |
markup |
a data frame of two columns containing the markup commands |
prompt |
whether to add prompts to the code |
fallback |
whether to use the fallback method, i.e. the regular
expression based method; this method is not precise and only highlights a
few types of symbols such as comments, strings and functions;
|
... |
arguments to be passed to |
For the markup
data frame, the first column is put before the R
tokens, and the second column is behind; the row names of the data frame must
be the R token names; a special row is named DEFAULT
, which contains
the markup for the standard tokens (i.e. those that do not need to be
highlighted); if missing, the built-in data frames highr:::cmd_latex
and highr:::cmd_html
will be used.
This function only binds markups onto R tokens, and the real syntax highlighting must be done with style definitions, which is out of the scope of this package. It was designed to be used as the syntax highlighting infrastructure of other packages such as knitr, where the colors and font styles are properly defined in the LaTeX preamble and HTML header.
A character vector for the syntax highlighted code.
Yihui Xie and Yixuan Qiu
See the package vignettes browseVignettes('highr')
for how
this function works internally.
library(highr) hilight("x=1 # assignment") txt = c("a <- 1 # something", "c(y=\"world\", z=\"hello\")", "b=function(x=5) {", "for(i in 1:10) { if (i < x) print(i) else break}}", "z@child # S4 slot", "'special chars <>#$%&_{}'") cat(hi_latex(txt), sep = "\n") cat(hi_html(txt), sep = "\n") # the markup data frames highr:::cmd_latex highr:::cmd_html
library(highr) hilight("x=1 # assignment") txt = c("a <- 1 # something", "c(y=\"world\", z=\"hello\")", "b=function(x=5) {", "for(i in 1:10) { if (i < x) print(i) else break}}", "z@child # S4 slot", "'special chars <>#$%&_{}'") cat(hi_latex(txt), sep = "\n") cat(hi_html(txt), sep = "\n") # the markup data frames highr:::cmd_latex highr:::cmd_html