Thanks for the response, @cderv. Calling an extra JavaScript library would certainly be overkill.
It strikes me that what I probably have, then, is a feature request for knitr
.
There's no need to load a whole JS library, it seems. Instead, all that is needed is some new functionality to add a CSS class to the echo of the code.
For example, let's say I knit a new Notebook with the following YAML header:
title: "R Notebook"
output:
html_document:
theme: spacelab
highlight: pygment
In the body of the notebook, I include just the following code chunk:
{r}
library(tidyverse)
df <- data.frame(var=character(), var2=character())
df %>%
select(var, var2) %>%
count(var2)
The resulting HTML file includes the following in the source for that chunk:
<pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb5-1" data-line-number="1">df <-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">var=</span><span class="kw">character</span>(), <span class="dt">var2=</span><span class="kw">character</span>())</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">df <span class="op">%>%</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3"><span class="st"> </span><span class="kw">select</span>(var, var2) <span class="op">%>%</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4"><span class="st"> </span><span class="kw">count</span>(var2)</a></code></pre>
What's missing is the ability to add an additional CSS class
to each line based on the data-line-number
attribute of the a
elements within the code
tag.
Then, a simple custom CSS file (which we can already include in the YAML header) could be used to style that class to set things like the background color of that line, or the text of the color for that line, and so on.
So, the new feature would be to add a new chunk option called something like stylinelines
, in which a vector of lines could be specified along with the additional style that would be set. Here's an illustration of what that would look like:
{r stylelines=list(c(3), "highlight")}
df <- data.frame(var=character(), var2=character())
df %>%
select(var, var2) %>%
count(var2)
Which, in turn, should be able to produce the following HTML:
<pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb5-1" data-line-number="1">df <-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">var=</span><span class="kw">character</span>(), <span class="dt">var2=</span><span class="kw">character</span>())</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">df <span class="op">%>%</span></a>
<a class="sourceLine highlight" id="cb5-3" data-line-number="3"><span class="st"> </span><span class="kw">select</span>(var, var2) <span class="op">%>%</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4"><span class="st"> </span><span class="kw">count</span>(var2)</a></code></pre>
And, within a CSS file, one would just add the following:
.highlight {
background-color: yellow;
}
Based on my quick testing (by manually editing the class for that one line in Firefox's Inspector), that is able to demonstrate the result shown in the opening post.
I'm sure implementing what seems like a simple feature would actually be pretty complicated, and the package maintainers may not find it worth the effort. However, if I were to make such a request, what would be the best way to direct it? The Issues page on knitr
's GitHub page?