How to combine multiple R-Markdown (.rmd) files into a single document?

Asked

Viewed 71 times

1

In a project on R, I have several files .Rmd in the same folder. As in the example below:

Capitulo1.Rmd

---
title: "Capítulo 1"
output: pdf_document
---

# Esse é o capítulo 1. 

```{r}
plot(cars)
```

Capitulo2.Rmd

---
title: "Capítulo 2"
output: pdf_document
---

# Esse é o capítulo 2. 

```{r}
plot(pressure)
```

How to merge/compile all chapters into a single file .pdf ?

Note: question and answer adapted from the Soen, in order to systematize the most recent knowledge on the subject to the Portuguese-speaking community, according to guidelines suggested by three debates of the Meta Sopt, in the links: 1, 2, and 3 .

1 answer

1

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).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.