Skip to content

Commit

Permalink
tweak demo page for #92
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Jan 20, 2012
1 parent fc00af0 commit 6b96785
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion _posts/2012-01-18-child.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ subtitle: Input child files into the main document
category: demo
---

_Warning: this page is under construction and should not be considered to be complete. The feature described here relies on the development version of **knitr** on GitHub. General users can start using this feature after this warning message is removed._

Like the command `\SweaveInput{}` in Sweave, **knitr** also has the similar functionality, which enables us to insert child documents into a main document. The idea is like the command `\input{}` or `\include{}` in LaTeX to manage a large document in smaller parts.

## Using `knit_child()` with inline R code

Sweave searches for `\SweaveInput{}` in the main document, and weaves the child documents, whereas **knitr** does not search for such character strings, instead, you can directly use `\Sexpr{}` with the function `knit_child()` to input a child document.

For example, we have a main document named [`knitr-input.Rnw`](https://github.com/yihui/knitr/blob/master/inst/examples/knitr-input.Rnw) here:
Expand Down Expand Up @@ -51,4 +55,17 @@ library(knitr)
knit('knitr-input.Rnw')
{% endhighlight %}

You can freely nest one Rnw document in the other, as long as the nested structure is clear in your mind. This sounds like the movie [Inception](http://en.wikipedia.org/wiki/Inception).
You can freely nest one Rnw document in the other, as long as the nested structure is clear in your mind. This sounds like the movie [Inception](http://en.wikipedia.org/wiki/Inception).

Be sure to read `?knit_child` to know more about the control of evaluation (argument `eval`), which is similar to what is described below in the second approach.

## Using chunk option `child`

The chunk option `child` supports the inclusion of child documents. The above example can be equivalently done with:

{% highlight r %}
<<child-demo, child=knitr-input-child.Rnw>>=
@
{% endhighlight %}

You can control the evaluation of such child chunks with the usual chunk option `eval=TRUE/FALSE`, therefore the feature of conditional evaluation can also be used here (i.e. you can evaluate the chunks according to a global variable; see the main manual). It is fairly flexible to control which child chunks to include into the main document. This is useful when a large report consists of smaller pieces, and different pieces may generate different reports.

0 comments on commit 6b96785

Please sign in to comment.