Prism syntax highlighting in R

This is a test of Prism syntax highlighting for R.

You need to include prism.r.js into your site, and also define CSS classes .token.function, .token.variable and (optionally) .token.namespace.

This is in addition to your chosen prism.min.js and stylesheet you download from the PrismJS website. (You may wish to minify prism.r.js and combine it into prism.min.js).

<script src="prism.min.js" type="text/javascript"></script>
<script src="prism.r.js" type="text/javascript"></script>
.token.function { ... }
.token.variable { ... }
.token.namespace { ... }

The CSS sheet used for this page is based off Chris Kempson's Tomorrow Night 80s palette, matching the one of the same name used by RStudio.

Example

If you add the "language-r" class to inline code it will also be marked up: x <- 1 + 2. The following is some block R code.

# This is a comment.

like <- function (times=1, like=T, ...) {
    str <- 'I do not like green eggs and ham!'
    if (like) str <- gsub('do not ', '', str)
    for (i in 1:10) {
        print(str)
    }
    return(paste(...))
}

x <- "Here is a
quite long
multiline \t \"
string with ' embedded.
"

x <- c(
    1e2 + 2i * 10,
    NA,
    NULL,
    Inf,
    NA_character_,
    TRUE,
    FALSE
)

[email protected]
list(a=1)$a[1]

?table

knitr::knit

Themes

Use the tab bar up top to switch between themes. The themes shown here are:

Known Issue

Hash characters (i.e. the symbol that you use to comment) within strings screw up highlighting. This is a typical problem of any regex-based syntax highlighter and is known by Prism.


x <- "Here is a string with a # inside."

There are a few workarounds you will try, though as with all regex-based solutions there will be cases where it doesn't work. Pick what suits you best.

If you like, you can swap the order of the 'comment' and 'string' lines in prism.r.js which will cause strings to take priority over comments. However, this means that if you have a comment with a string in it, it will fail.


x <- "Here is a string with a # inside." # and here is a comment with "quotes" in it

Alternatively, you can change the 'comment' regex to this:


Prism.languages.r = {
	'comment': /#(?=(?:[^"\\\r\n]*(\\.|"(?:[^"\\\r\n]*\\.)*[^"\\\r\n]*"))*[^"\r\n]*$).*$/gm,
	// ... the rest

This means that a hash tag within double quotes will not be highlighted. Also, if a comment has balanced double quotes in it it will be highlighted properly as a comment. However, if a comment has unbalanced double quotes in it (i.e. an odd number), that comment will not be highlighted properly. Also, if your string is surrounded by single quotes (not double) and has a hash tag in it, you will suffer from the original problem. (If you modify the regex above to allow hash tags within single quotes, you will have to be careful never to have unbalanced single quotes (i.e. apostrophes!) in your comments or else the comments won't be highlighted. Words with apostrophes are so common that this is a Bad Idea).

Again, regex highlighters always have these problems. If you think you never have an unbalanced double quote in a comment this could be a feasible solution, but pick according to your usage.


x <- "Here is a string with a # inside." # and here is a comment with "quotes" in it
"multiline string with
a hash tag # inside it"
# comment with unbalanced quote mark: " inside. Won't highlight.

Bugs, Suggestions and Improvements