HTML-Class Demo

Here are some (newer) options you can use with knitr (>= v1.15.6), class.source and class.output. Both are useful only for rendering R Markdown files into HTML. Multiple classes can be set by providing a vector, or a space-delimited string, i.e. class.source = c("foo", "bar") or class.source = "foo bar".

One way this can be useful is motivated by the original Stack Overflow question, how can we use knitr to help us to emphasize "bad" coding practices?

Using Bootstrap

Using R Markdown, the default behavior is to include the Bootstrap framework. This can make things very easy for you to signify your code and your output. Bootstrap defines some CSS classes for backgrounds: "bg-primary", "bg-success", "bg-info", "bg-warning", and "bg-danger".

For example, we are warned that the normal subsetting of a data frame can be type-unstable. To show this here, the chunk options are set: class.source = 'bg-warning', class.output = 'bg-warning'.

mtcars[, "mpg"]

To make sure that we return a data frame, we set the argument drop = FALSE. Here the chunk options are set: class.source = 'bg-success', class.output = 'bg-success'.

mtcars[, "mpg", drop=FALSE]

Using Custom CSS

You may wish to set your own CSS -- knitr and rmarkdown make this easy for you. If you are using the rmarkdown package, you can include your CSS file by setting a parameter in the YAML header (reference). You can also include a chunk of CSS code, as is done here.

Note that for this example, only class.source is set.

```{css echo=FALSE} .bad-code { background-color: salmon; }

```r
mtcars[, "mpg"] 


Try the parsermd package in your browser

Any scripts or data that you put into this service are public.

parsermd documentation built on May 20, 2021, 5:08 p.m.