Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knitr_child throws error after upgrade to R 3.0

Tags:

r

knitr

sweave

A script that has been running seamlessly for over a month stopped adding my child Latex code into my main document following an upgrade to R 3.0.1. The following snippet used to include the text from the compiled test.rnw file in my main document (so that it could be compiled as one document). Now it just includes the filenames of the compiled rnw files.

<<run-all, include=FALSE>>=
    out = NULL
    for (i in 1:10) {
      out = c(out, knit_child('test.rnw', sprintf('test-template-%d.tex', i)))
    }
@

\Sexpr{paste(out, collapse = '\n')}

When I try to run the knit_child command interactively, I get this error:

> knit_child('test.rnw', sprintf('test-template-%d.tex', i))
Error in setwd(opts_knit$get("output.dir")) : character argument expected

Running knit() alone will compile the Latex code, if I then run knin_child() there is not error but the "out" object just contains the filename of the child file instead of the contents.

Any ideas how to fix this?

like image 875
midden Avatar asked May 30 '13 04:05

midden


1 Answers

You are not supposed to use knit_child() interactively. It was designed to be called inside knit().

As you have noted, knit_child() in the latest version of knitr returns the content of the child document if you do not provide the second argument. By explicitly providing the second argument sprintf('test-template-%d.tex', i), you mean "please write the output to this file and return the filename".

To fix the problem, you need to remove the second argument:

<<run-all, include=FALSE>>=
    out = NULL
    for (i in 1:10) {
      out = c(out, knit_child('test.rnw'))
    }
@

\Sexpr{paste(out, collapse = '\n')}
like image 91
Yihui Xie Avatar answered Oct 17 '22 12:10

Yihui Xie