Examples

Begin by quietly defining a named chunk which will be stored in the knitr cache.

    ````{r example, eval=FALSE, echo=FALSE}
print('hello world!')
print('hello again!')
    ````

Default usage:

    ````{r example}
print('hello world!')
print('hello again!')
    ````
print('hello world!')

[1] "hello world!"

print('hello again!')

[1] "hello again!"

Global default comment set to NA:

Set the global default for the comment param to more closely match console ouput using opts_chunk$set(comment=NA)

    ````{r example}
print('hello world!')
print('hello again!')
    ````
print('hello world!')

[1] "hello world!"

print('hello again!')

[1] "hello again!"

Override global default comment:

    ````{r example, comment="override > "}
print('hello world!')
print('hello again!')
    ````
print('hello world!')

[1] "hello world!"

print('hello again!')

[1] "hello again!"

Output combinations

No echo:

    ````{r example, echo=FALSE}
print('hello world!')
print('hello again!')
    ````

[1] "hello world!" [1] "hello again!"

Collapsed:

    ````{r example, collapse=TRUE}
print('hello world!')
print('hello again!')
    ````
print('hello world!')

[1] "hello world!"

print('hello again!')

[1] "hello again!"

Held:

    ````{r example, results='hold'}
print('hello world!')
print('hello again!')
    ````
print('hello world!')

[1] "hello world!"

print('hello again!')

[1] "hello again!"

Collapsed & held:

    ````{r example, collapse=TRUE, results='hold'}
print('hello world!')
print('hello again!')
    ````
print('hello world!')

[1] "hello world!"

print('hello again!')

[1] "hello again!"

Echo with no-eval followed by no-echo held results:

    ````{r example, echo=TRUE, eval=FALSE}
print('hello world!')
print('hello again!')
    ````
    ````{r example, echo=FALSE, results='hold'}
print('hello world!')
print('hello again!')
    ````
print('hello world!')
print('hello again!')

[1] "hello world!"
[1] "hello again!"

Echo with no-eval followed by no-echo and collapsed results:

    ````{r example, echo=TRUE, eval=FALSE}
print('hello world!')
print('hello again!')
    ````
    ````{r example, echo=FALSE, collapse=TRUE}
print('hello world!')
print('hello again!')
    ````
print('hello world!')
print('hello again!')

[1] "hello world!"
[1] "hello again!"

Hopefully it worked.