# knitr-setup should be the first chunk in the document.
knitLiteral::kast_on()

About

The knitLiteral package gives authors the ability to easily output the exact the source of a chunk without side-effects. It is simple to use; just remember that knitr processes chunk options as they occur and options can occur more than once.

The main option that knitLiteral allows is literal=TRUE. This option allows you to generate the output you literally want to use in knitr to produce a result without repeating yourself or breaking the parser. (Notice the use of 4 backticks instead of the minimal 3 to allow R syntax highlighting to work.)

If the literal label is used more than once in a particular chunk and the options end with literal=TRUE then only the final literal=TRUE will be removed, which allows for illustrating how the literal chunk was produced.

The rmarkdown in this sample document should produce very similar results using either rmarkdown v1 or v2 and using pandoc to generate Word, pdf or html output. Other output formats have not been tested.

Setup

It is very easy to initialize using kast_on in your first chunk.


It is very easy to style using dye in your closing chunk.

# include-after should be the last chunk in the document.
knitLiteral::dye()

Examples

Each sample shows the Source along with the rendered Literal and normal Output.

No output, caching only

Source Following the DRY principle this is the source for all "example" chunks.

print('hello world!')
print('hello again!')

Literal


Output (none)

Default

Source


Rendered


Default comment

Set the global default comment option to match normal console output using opts_chunk$set(comment=NA).

r knitr::opts_chunk$set(comment=NA)

Source


Rendered


Chunk comment

Source


Rendered


No echo

Source


Rendered


Collapse

Source


Rendered


Hold {#Hold}

Source


Rendered


Collapse & hold

Source


Rendered


Echo & no-eval, no-echo & hold combo

Source





Rendered (the normal output is the same as a simple hold)





Echo & no-eval, no-echo & collapse combo {#MDExample}

Source





Rendered





Silent Tidy of training chunk.

Imagine having a chunk to use for training, but it needs tidying, and the tidying has nothing to do with the training. By taking advantage of how knitr handles chunk options a chunk can be silently turned into a nice example.

Starting with a cached chunk like this

# This should not be displayed, it is just a note to myself

# that this coding is horrible and would be _wrong_.

##' Without a doubt!

if ( ( knitr::opts_current$get("literal") == TRUE ) && ( knitr::opts_current$get("literal") == TRUE ) )
  {
  foo<-FALSE } else {
    foo = TRUE
  listfoo <- list( first="second", second="third", third="fourth", fourth=fifth, and="so on")
}

Make a new options template like this

knitr::opts_template$set( "literal-tidy"=
  list( literal=TRUE, echo=FALSE, eval=FALSE, tidy=TRUE,
        tidy.opts=c( width.cutoff=50, keep.comment=FALSE,
                     keep.blank.line=FALSE, replace.assign=TRUE)))

And use it like this


To get results like this


Which is a much better, although still bad, example.

knit markdown output.

If you are interested in keeping a clean and readable markdown file the following is the knitr generated markdown from the Echo & no-eval, no-echo & collapse combo example.

````r
````
````r
````
````r
````
````r
````

Styling Information

HTML Classes

The knitr chunk pre class and code class.

~~~~~~~~~~~{.html}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

CSS

The style for the knitr blocks was accomplished by simply using four back ticks instead of three so the R syntax highlighter would work and by defining a style.

~~~~~~ {.css}

css <- system.file("css/literalize.css", package = "knitLiteral")
css <- readLines(css)
cat( css, sep="\n" )
## Javascript for rmarkdown v1

~~~~~~ {.javascript}
````r
js <- system.file("js/literalize.js", package = "knitLiteral")
js <- readLines(js)
cat( js, sep="\n" )
````



Thell/knitLiteral documentation built on May 9, 2019, 4:28 p.m.