Bookdown package
One option is to use the package bookdown, written especially for the writing of long books and reports in the R-markdown, and which includes various features such as the possibility of cross-references between chapters.
To join the various files together .Rmd
, create a new file named index.Rmd
in the same folder where the other files are located .Rmd
of the project. In this file index.Rmd
, we should include an additional line in YAML: site:bookdown::bookdown_site
. When we compile the file index.Rmd
the bookdown package will join all the files .Rmd
that are in the same folder, in alphabetical order. There are two exceptions: the first is the file index.Rmd
, always the first to be compiled. The second is the files started with subscript, for example _teste.Rmd
, who are ignored.
index. Rmd
---
title: "Meu livro escrito no R"
site: bookdown::bookdown_site
output:
bookdown::pdf_document2:
toc: yes
---
# Prefácio:
Capitulo1.Rmd
---
title: "Capítulo 1"
output: pdf_document
---
# Capítulo 1
```{r}
plot(cars)
```
Capitulo2.Rmd
---
title: "Capítulo 2"
output: pdf_document
---
# Capítulo 2
```{r}
plot(pressure)
```
Choosing which files to compile and modifying their order
If you want to join the files in a different order from the alphabetical order, or leave some file .Rmd
of the project outside the final compilation, it is possible to do this by changing the file _bookdown.yml
. In this case, let’s suppose that you want to send to a study group only the preface and chapters 2 and 5 of your book. You can do this by adding the field rmd_files
in the archive _bookdown.yml
:
rmd_files: ["index.Rmd", "Capitulo2.Rmd", "Capitulo5.Rmd"]
This strategy is especially useful if you want to produce two publications in different formats using files .Rmd
of the same project. For example, if you want to publish your book/report in HTML and also in PDF, you can specify a different sequence of files to be compiled for each format:
rmd_files:
html: ["index.Rmd", "Capitulo1.Rmd", "Capitulo2.Rmd"]
latex: ["index.Rmd", "Capitulo1.Rmd", "Capitulo2.Rmd", "ZAnexoA.Rmd"]
For more details, see the documentation of the bookdown package (additional source of information in this reply).