Home Comments Thread
New Thread

[knitr] Child documents: Input child files into the main document | /knitr/demo/child/

yihui yihui 2022-12-16 22:22:23

Child documents: Input child files into the main document

https://yihui.org/knitr/demo/child/

13 Comments

giscus-bot giscus-bot 2022-12-16 22:22:24
Guest *jamiefolson* @ 2012-05-15 18:04:42 originally posted:

Is it intentional that child .tex documents are always created in the base directory even if they're in subdirectories?

yihui yihui 2022-12-16 22:24:37

Yes, because it makes it easy to handle figure files (relative directories in the R level and LaTeX level can be easily messed up). I'm thinking of not writing child.tex at all -- just write all the texts back into the main document directly.

Originally posted on 2012-05-15 19:09:20

giscus-bot giscus-bot 2022-12-16 22:22:25
Guest *Jared* @ 2012-05-30 02:58:53 originally posted:

Loving knitr for everything from making a book, to an article to a presentation.  I'm trying to be more sophisticated about child documents (right now I wrote a function that checks all listed child documents and only knits them if the Rnw files have changed, then use include{} to pull them into the master Rnw file).

I'm trying out <<child-sec,child='folder file.rnw',eval="TRUE">>= but I have two questions/problems.

First, when each file is in a different, non-nested, folder only the first one works because the working directory has been changed.  The comment by Jamie makes it seem like the Rnw files can be stored in different directorys but I can't make it work.

Second, when using the above command, does it only compile the Rnw to tex when the Rnw has been changed, or does it compile every time?  That can be a big cost saving.

Thank you for the amazing work and any answers you can provide.

yihui yihui 2022-12-16 22:24:38

You need to use the development version on github: https://github.com/yihui/knitr The CRAN version has a bug for child documents.

For the second question, if you want to avoid computation, you can enable cache (set chunk option cache=TRUE); otherwise it is compiled every time.

Originally posted on 2012-05-30 03:14:41

giscus-bot giscus-bot 2022-12-16 22:24:39
Guest *Jared* @ 2012-05-30 03:41:54 originally posted:

Thanks for the quick reply.  I'll get the version off GitHub and give it a shot.

I still haven't gotten the hang of cache, but I will give it a try.

Once again, I love knitr.  Looking forward to meeting you at the NY meetup.

giscus-bot giscus-bot 2022-12-16 22:24:40
Guest *Jared* @ 2012-05-30 15:32:04 originally posted:

Should I be caching the chunk that does the actual work:
<<the-child, eval="TRUE,cache=TRUE">>=

Or the chunk that calls the child:
<<in-master, child="the-child" ,eval="TRUE,cache=TRUE">>=

Or something else entirely?

yihui yihui 2022-12-16 22:24:41

Both are fine, but the second approach will surely be faster since the child document will be totally ignored while the first approach still needs to parse the child document to realize the chunks are cached.

Originally posted on 2012-06-01 20:42:35

giscus-bot giscus-bot 2022-12-16 22:24:42
Guest *Jared* @ 2012-06-04 01:54:57 originally posted:

In the master document I did
<<graph,child='chunk2 chunk2.rnw',cache="TRUE,eval=TRUE">>=@

But it didn't create a cache and the tex file is rewritten every time.  Anything I'm doing wrong?

yihui yihui 2022-12-16 22:24:43

Oh, you are right. The second approach will not work. You need something like this:

<<in-master, cache=TRUE, include=FALSE>>=
child = knit_child('the-child.Rnw')
@
Sexpr{child}

Originally posted on 2012-06-04 03:34:38

giscus-bot giscus-bot 2022-12-16 22:24:44
Guest *Jared* @ 2012-06-04 04:59:14 originally posted:

That works when there is not a cache already existing.  If the cache exists it prints this error to the document.
Error in eval(expr, envir, enclos) : object 'child' not found
I have seemed to notice that I have trouble accessing objects in the cache.

giscus-bot giscus-bot 2022-12-16 22:24:45
Guest *Jared* @ 2012-06-04 05:01:16 originally posted:

Plus, if I change the file, but not the chunk that does child = knit_child('the-child.Rnw'), then knitr will think to still use the cache because as far as it is concerned the chunk hasn't changed.

yihui yihui 2022-12-16 22:24:46

I recommend you to use the original approach (in-master, child='the-child', eval=TRUE, cache=TRUE); even if the child document is parsed, it should not be too slow because the computation is skipped when you have set cache to be TRUE globally (opts_chunk$set(cache=TRUE)).

Originally posted on 2012-06-04 14:29:25

giscus-bot giscus-bot 2022-12-16 22:22:27
Guest *Martin* @ 2012-06-07 12:20:57 originally posted:

I was wondering if there is a way to include child documents when using Markdown instead of LaTeX?
Thanks for a great tool! 

yihui yihui 2022-12-16 22:24:47

I think it is entirely possible; please file a feature request at https://github.com/yihui/knitr/issues Thanks!

Originally posted on 2012-06-07 13:16:22

giscus-bot giscus-bot 2022-12-16 22:24:47
Guest *Martin* @ 2012-06-07 13:50:30 originally posted:

That would be a great additional feature - I have submitted it as as feature request.
Thanks!

giscus-bot giscus-bot 2022-12-16 22:22:28
访客 *Rapheal210* @ 2012-06-13 05:40:40 写道:

谢兄,你好。关于“Knit child documents in a standalone mode”部分,我有点没看明白。到底set_parent函数应该放在什么位置?是放在child doucument里面么?
比如把child document写成下面的样子:<<set-parent, echo="FALSE," cache="FALSE">>=
set_parent('knitr-input.Rnw')
@
<<test-child, out.width="2in">>=
1+1
rnorm(5)
plot(1)
boxplot(1:10)
str(mtcars)@ 是这样么?但是这样好像child document还是无法单独编译啊?还是我的理解有误?
 

yihui yihui 2022-12-16 22:24:48

这写法是对的。参见 https://github.com/yihui/knitr/tree/master/inst/examples/child 下的knitr-main.Rnw和knitr-parent.Rnw

注意@需要换行。

——原帖发布于 2012-06-13 05:51:33

giscus-bot giscus-bot 2022-12-16 22:24:49
访客 *Rapheal210* @ 2012-06-13 07:10:14 写道:

 我把你的demo下载了在本机运行,其它例子都运行正常。但在knitr-parent.Rnw运行出来的结果很奇怪,只有一个标题(见附图)。是版本的问题么?
我在windows系统下。

giscus-bot giscus-bot 2022-12-16 22:24:50
访客 *Rapheal210* @ 2012-06-13 08:00:47 写道:

 我把你的例子下载到本地运行。其它的都运行正常,除了
knitr-parent.Rnw。

把knitr-parent.Rnw文件knit之后得到的pdf文件中只有一行文字,就是标题行“Child can specify its parents”。
这是怎么回事?是版本问题么?
我是version0.5,在windows下。

giscus-bot giscus-bot 2022-12-16 22:22:29
Guest *Rafik Margaryan* @ 2012-10-18 11:07:51 originally posted:

Dear Yihui Xie
I have created one Rproject multiple Rnw files under different subdirectories. 
for knit-ing I use knit("subdir1/ex1.Rnw","subdir1/ex1.tex"). 
I would like to compile ex1.tex in its directory(subdirectory of main project). 
Compiling with Rstudio compilePDF gives me error of not finding figures which I also put under subdir1/fig . 
How I should manage this multiple Rnw files within R (preferably). 
Many thanks

yihui yihui 2022-12-16 22:24:51

Always put output files under the current working directory if your source documents have graphics or cache output, otherwise the file structure will be extremely messy, because all these auxiliary files are written relative to the current working directory.

Although I can (and probably will) fix this problem, I still prefer you working under "subdir1/" and knit("ex1.Rnw"). If not absolutely necessary, do not specify the output file to be under another directory; knitr is not yet intelligent enough to move the graphics output there.

Originally posted on 2012-10-19 05:03:23

giscus-bot giscus-bot 2022-12-16 22:22:30
Guest *hugo koopmans* @ 2012-11-13 14:52:10 originally posted:

Hello Yihui Xie,

First a big thank you for your great work!

Intro: I would like to create an automated data quality report builder in R + knitr. Using R Studio at the moment and I have played around with it all. The point is that I would like to read in an arbitrary dataset and then for each variable do some analysis in R plus some tex stuff describing the field, missing values may be a histogram plot ect ect. So if the variable is nummeric I wuld like to plot a histogram also some text on outliers and missing values . If the variable is a factor I would like to do something completly different. But in thee end I would like to knit a pdf docuemnt that holds high level overview plus for each variable a section or chapter in the pdf. I am puzzle how to generate rnw file for each variable...

My question: Can I use knitr (child documents?) to generate a document page for each variable in a dataset?

option: Is there a way to template rnw files so I can give a certain R variable to it?

hugo

yihui yihui 2022-12-16 22:24:52

The plot part is not relevant to knitr; it is a pure R question.

For the template question, you can take a look at this example:

https://github.com/yihui/knitr-examples/blob/master/020-for-loop.Rnw
https://github.com/yihui/knitr-examples/blob/master/020-for-template.rnw

Originally posted on 2012-11-13 20:03:26

giscus-bot giscus-bot 2022-12-16 22:22:31
Guest *Henrik Nyhus* @ 2013-01-28 07:54:24 originally posted:

Hi Yihui, thanks for this great package!

I'm having some trouble weaving a child

<<titlepage, child="titlepage.Rnw" ,="" eval="TRUE">>=
@

containing utf-8 encoded letters (norwegian - ÆØÅ) with the "Complie PDF" function in Rstudio.

The outputs .tex (and .pdf) files read for instance "" instead of "ø".

When I add all the text from the child direct into the main .Rnw file, it compiles normally. Using the command "knit" it also typesets the .tex file properly, which I then externally can compile to a .pdf, which probably would imply that the problem lies on Rstudio rather than knitr.
BTW: The "Compile PDF"-funciton in Rstudio puts all "childs" in ONE .tex/ file, where as "knit"/"knit2pdf creates a .tex for each .Rnw.

All files and preambles for the utf-8 encoding is regarded.

Being new to compiling LaTex in Rstudio, I am not sure whether it's due to user, R(studio) or knitr error.

I could not find any information on anyone experiencing the same issues, and thought it would be interesting for you to know about it.
Perhaps something you already have heard of?

Beste Regards,
Henrik

ps! There ist some additional code appering after posting this that obviously is not containes in the "original" .Rnw file.

yihui yihui 2022-12-16 22:24:53

Thanks for the bug report. The encoding problem should be fixed now, and please test the development version of knitr: https://github.com/yihui/knitr#readme

RStudio produces one tex file because you have turned on the concordance option in Tools--Options--Sweave, and this option is disabled in knitr by default.

Originally posted on 2013-01-28 19:48:12

giscus-bot giscus-bot 2022-12-16 22:22:31
Guest *Paul* @ 2013-02-07 11:52:41 originally posted:

This is all good and dandy but it doesn't work with AucTeX in Emacs. RefTeX knows about commands like include and input and follows them to build a comprehensive table of contents nor navigation. I tried to create a workaround with

newcommand{kinclude}[1]{
Sexpr{knit_child("#1")}
}

and then add a child with

kinclude{introduction.Rnw}

This doesn't work, though. I get the following error:

Error in auto_out_name(input) : cannot determine the output filename automatically introduction.Rnw

yihui yihui 2022-12-16 22:24:55

That seems to be a common misunderstanding: http://stackoverflow.com/a/14681064/559676

Originally posted on 2013-02-08 03:56:45

giscus-bot giscus-bot 2022-12-16 22:24:56
Guest *Paul* @ 2013-02-08 07:23:29 originally posted:

I see. For the time being, I use the following workaround:

Sexpr{knit_child("introduction.Rnw")}
kinclude{introduction.Rnw}

where kinclude does nothing and is just a hint for RefTeX to expand the TOC. Thank you for the quick response (and for knitr, obviously)!

yihui yihui 2022-12-16 22:24:57

That is pretty smart! :)

Originally posted on 2013-02-08 16:35:37

giscus-bot giscus-bot 2022-12-16 22:22:32
Guest *onichi* @ 2013-02-07 16:17:30 originally posted:

Thanks for knitr, Yihui!

Is there anyway to do selective prints if a document is detected to be the parent as opposed to a file? I have some children files I would like to be able to structure as separate documents, with their own pre-ambles, etc. and then be able to compile them into full documents.

However, if they are included as a child, it will use whatever the parent document has.

Thank you!

yihui yihui 2022-12-16 22:24:54

That is a reasonable feature, so please write it here https://github.com/yihui/knitr/issues and I'll try to implement it once I have got time. Thanks!

Originally posted on 2013-02-08 03:03:31

giscus-bot giscus-bot 2022-12-16 22:22:33
Guest *Petri Koistinen* @ 2013-03-06 10:24:47 originally posted:

I am in the process of converting a largish LaTeX document so that it uses knitr. I am having trouble with child documents, which I definitely want to include{}, not input{} in the master file. If my master file master.rnw looks something like this:

<<>>=
opts_knit$set(child.command = 'include')
@

<<child=c('intro.rnw', 'other.rnw')="">>=
@

and I run knit(), then I will get the file master.tex, which looks like this

include{intro.tex}

include{other.tex}

This creates havoc when I run LaTeX. The correct format for include{} is to omit the .tex externsion, like this:

include{intro}

include{other}

Is there an easy fix to this problem?

Thank you!

yihui yihui 2022-12-16 22:24:57

Hi, this problem has been fixed in this commit: yihui/knitr@c3d1c10

You can install the development version to test it: https://github.com/yihui/knitr#readme Thanks!

Originally posted on 2013-03-07 06:00:58

giscus-bot giscus-bot 2022-12-16 22:22:34
Guest *Roman Ahmed* @ 2013-07-24 00:42:20 originally posted:

Hi Yihui,

Thanks for developing this awesome package .... love it.

I have a question regrading child document specially it's evaluation and storing results.

I want to use child documents as the sections of an article. My problem is computing time for few sections are very long (few days). Therefore I want to make sure once the parent document is evaluated the results for the child document is stored there and when I texify the same parent document next time with an additional section (child) it doesn't evaluate the codes for the previously evaluated child but prints the results.

Definitely I am assuming independence between sections/childs in terms of computing.

Is there any way to do that? Cheers, Roman

yihui yihui 2022-12-16 22:24:59

Did you turn on the option cache=TRUE in your child documents?

Originally posted on 2013-07-24 03:15:30

giscus-bot giscus-bot 2022-12-16 22:24:36
Guest *Doihanh* @ 2013-10-01 23:06:08 originally posted:

Hi Yihui, I am compiling a big document containing several child documents. One question: How can I include references for each chapter which are child documents? What I got now is all chapters have the same list of references. Thanks, Doi.

yihui yihui 2022-12-16 22:25:00

That sounds like a pure LaTeX problem that is orthogonal to knitr, and I think Google is your friend.

Originally posted on 2013-10-02 18:56:32

giscus-bot giscus-bot 2022-12-16 22:24:36
Guest *ADPowers* @ 2013-11-02 22:40:20 originally posted:

Yihui, I have a large document (100+ pages) which is 99.99% plain LaTeX, however, I want to include maybe one or two knitr Chunks. What is the best way to do this, without converting every file to .Rnw and knitting as per usual.

Sign in to join the discussion

Sign in with GitHub