Pretty R is an online tool and r syntax highlighter that transforms R source code into HTLM code for website development. The result is easy to read R code for high quality web presentations. The Pretty R webpage is a good learning tool as it provides the HTML code details required to deliver syntax highlighting that complies with R documentation from inside-r.org.
In practice, the use of a website translator is not the most efficient way to convert R into syntax highlighted HTLM. The brute force approach is to write HTML or CSS code directly, layering complex web syntax into the analytic code. For me, I have enough trouble making sure the technical code works, and works efficiently, let alone doing dual-purpose development for analysis and web posting.
R syntax highlighters are reviewed below, benefiting from hands-on trial and error.
R Mardown is another approach for converting raw source code for web pages, but R Markdown requires the creation of a separate text or *.rmd document. The text code document serves as a bridge, where R is translated into HTML using a 3rd party package and utilities to interface with your website. Again, the requirement is to manually embed R “code chunks” for analysis into code for web translation.
Syntax Highlighter Evolved is one solution appropriate for websites built using the WordPress.org framework. The plug-in converts R code into HTML code quickly and it easily separates analytic work from web design. Basic usage involves dropping R code direct into the HTML text file for the website and wrapping the code as follows:
The mandatory parameter language, or lang for short, must be a supported language, like R or some other language, that requires HTML conversion. The list of supported languages and optional parameters that can be inserted into the source code tags can be found here.
Crayon Syntax Highlighter is the solution used here. It offers more control compared to other syntax highlighters for WordPress, and the code formats can be easily customized. I suppose its a matter of taste, but when I saw Crayon for the first time I didn’t hesitate to switch from other tools. The presentation quality is higher and the code blocks include menu items to facilitate user interface.
Switching from Syntax Highlighter Evolved was relatively straightforward. It required changing code tags across the website since the basic syntax for Crayon is:
I opted for a manual conversion since the Crayon code editor supports a range of code control options. The result is that raw source code in text format is converted into HTML code presentations like this:
# Earth orbit around Sun in days
orbit.days <- 365.2563666
# Earth distance from Sun in km at perihelion (closest)
per <- 149600000
# Earth orbit elliptical eccentricity
e <- 0.017
# Date and hour of perihelion for 2014 supplied by the US Naval Observatory
per.date <- as.POSIXct("2014-01-02 12:00:00")
# Theta angle of Earth orbit to perihelion in radians as a function of date
seq.date <- seq(as.POSIXct("2014-01-01", tz="GMT"), by="days", length=365)
per.diff <- as.numeric(difftime(seq.date, per.date, units="days"))
theta <- (per.diff * (360/orbit.days)) * pi/180
# Earth distance from Sun as a function of date; scaled to 100 million km
r <- (per * (1-e^2)) / (1 + e * cos(theta)) / 100000000