Update to highlightHTML package

March 14, 2014
By

(This article was first published on Educate-R - R, and kindly contributed to R-bloggers)

I've added a new functionality to my highlightHTML package. This package post-processes HTML files and injects CSS and adds tags to create some further customization (for example highlight cells of a HTML table). This is most useful when writing a document using markdown and converting it into a HTML document using a tool like knitr, slidify, or even pandoc.

Up to now, my package only worked with tables, see my old post that talks about this if you are interested: highlight tables. My update adds a similar functionality to text itself by including span tags in the document.

The following code will install the package with the new feature from github:

library(devtools)
install_github(repo = "highlightHTML", username = "lebebr01", ref = "testing")

Once the package is installed, the new function is called highlightHTMLtext. This function takes a HTML file as the input and post processes the file to add span tags to format text according to the CSS calls specified by the user. The function looks for {#id text} to add the span tags. The braces are used to define the text range that will use the id and the #id is the CSS id itself.

Here is an example using the HTML file that comes with the package and which can also be found in the help file.

library(highlightHTML)
file <- system.file('examples', package = 'highlightHTML')
file1 <- paste(file, "bgtext.html", sep = "/")

# Change background color and text color with CSS
tags <- c("#bgblack {background-color: black; color: white;}",
  "#bgblue {background-color: #0000FF; color: white;}")

# Post-process HTML file
highlightHTMLtext(input = file1, output = NULL, updateCSS = TRUE,
  tags = tags, browse = TRUE)

If you run the above command, the file should open in your browser to see the result of the new HTML file. The result should have boxes of color in specific areas that we indicated by the {#id text} syntax in the raw markdown and HTML file.

My next step is to develop a master function to wrap these other functions so only one call would be needed to format text and tables. Let me know of any issues by going to the github page: report bugs.


Before and After HTML

Here is what the body of the HTML file looks like before running the function:

<body>
<h1>Test of Text</h1>
<p>Testing the ability to change the {#bgblue color} of the text.</p>
<p>Can also do {#bgblack multiple words of text}</p>
<p>{#bgblack Even entire paragraphs that you want to really stand out from the rest of the document.  More than color could also be changed, anything alterable by CSS.  Test out the function and get creative with the CSS}</p>
</body>

This is what the HTML document looks like after running the function:

<body>
<h1>Test of Text</h1>
<p>Testing the ability to change the <span id='bgblue'> color</span> of the text.</p>
<p>Can also do <span id='bgblack'> multiple words of text</span></p>
<p><span id='bgblack'> Even entire paragraphs that you want to really stand out from the rest of the document.  More than color could also be changed, anything alterable by CSS.  Test out the function and get creative with the CSS</span></p>
</body>

The braces identify the location of the span tags and the custom CSS id tag to format the text.

To leave a comment for the author, please follow the link and comment on his blog: Educate-R - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.