5

I have a master R markdown document (Rmd) within which I would like to knit several separate Rnw documents (NO child documents) in one of the chunks. However, when I call knit on the Rnw document, the contained R code chunks do not seem to be processed, resulting in an error when trying to run texi2pdf on them.

Illustration of the situation:

Inside master.Rmd:

```{r my_chunk, echo=FALSE, message=FALSE, results='asis'}

... some code ...

knit("sub.**Rnw**", output = ..., quiet = TRUE)

tools::texi2pdf(tex_file)

... some code ...


```

Is there some additional configuration required to make this scenario work?

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
Martin Studer
  • 2,213
  • 1
  • 18
  • 23
  • +1 if at the very least as a hat tip for XLConnect. – Brandon Bertelsen Feb 10 '14 at 22:44
  • 1
    Not good solution but you can do it by invoking new process from .Rmd: `system("R -e \"knitr::knit('sub.Rnw')\"")` – kohske Feb 12 '14 at 00:29
  • 1
    A MWE would be helpful here. Do you want the final document to include the output from knit("sub.Rnw", ...), or are you just generating separate pdfs from a single master Rnw file? – Tyler Feb 20 '14 at 14:34
  • The idea is to generate separate PDFs from a single master Rmd page – Martin Studer Feb 20 '14 at 20:30
  • If you provide short, complete Rmd and sub.Rnw documents it makes it much easier for someone else to debug your issue. With a single incomplete example file, you leave it for us to guess what you've done. – Tyler Feb 26 '14 at 12:00
  • seems weird to want to output several documents from a .Rmd. Why don't you output your documents from a master script, that will knit all your documents ? – Karl Forner Mar 22 '14 at 12:44

1 Answers1

3

There are a few reasons you can't directly do what you are trying to do (calling knit from within a knit environment)...

  1. Knitr patterns are already set.
    [ In this case markdown patterns, so you'd need to set the patterns to 'rnw' patterns. ]
  2. Parsing the chunks (after setting the correct patterns) will add chunk labels to the existing concordance, so unless all chunks are unique you will get a duplicate chunk label error.
    [ This is why knit_child exists. ]
  3. The output target and other options are already set, so you either need a completely new knitr environment or to save, modify, restore all pertinent options.

That being said, it seems like completely expected behavior.

Something along the lines of

library(knitr)

files <- list.files( pattern = "*.Rnw", path = ".")
files

## [1] "test_extB.Rnw" "test_ext.Rnw"

for( f in files ) {
  system( paste0("R -e \"knitr::knit2pdf('", f, "')\"") )
}

list.files( pattern="*.pdf", path=".")

## [1] "test_extB.pdf" "test_ext.pdf"

or calling Rscript in a loop should do the trick (based on the info provided), which is essentially what @kohske was expressing in the comments.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Thell
  • 5,883
  • 31
  • 55