如何将两个RMarkdown(.Rmd)文件合并到一个输出中? [英] How to combine two RMarkdown (.Rmd) files into a single output?

查看:785
本文介绍了如何将两个RMarkdown(.Rmd)文件合并到一个输出中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一文件夹中有两个文件:Chapter1.Rmd和Chapter2.Rmd,具有以下内容:

I have two files in the same folder: chapter1.Rmd and chapter2.Rmd, with the following content:

chapter1.Rmd

---
title: "Chapter 1"
output: pdf_document
---

## This is chapter 1. {#Chapter1}

Next up: [chapter 2](#Chapter2)

chapter2.Rmd

---
title: "Chapter 2"
output: pdf_document
---

## This is chapter 2. {#Chapter2}

Previously: [chapter 1](#Chapter1)

我该如何编织它们以便将它们组合成一个pdf输出?

How can I knit these so that they combine into a single pdf output?

当然,render(input = "chapter1.Rmd", output_format = "pdf_document")可以完美地工作,但render(input = "chapter1.Rmd", input = "chapter2.Rmd", output_format = "pdf_document")则不能.

Of course, render(input = "chapter1.Rmd", output_format = "pdf_document") works perfectly but render(input = "chapter1.Rmd", input = "chapter2.Rmd", output_format = "pdf_document") does not.

我为什么要这样做?将大型文档分解成逻辑文件

Why do I want to do this? To break up a giant document into logical files.

我已经使用@hadley的 bookdown 包从.Rmd构建乳胶,但这似乎是对这个特定任务的矫kill过正有没有使用我遗忘的knitr/pandoc/linux命令行的简单解决方案?谢谢.

I've used @hadley 's bookdown package to build latex from .Rmd but this seems like overkill for this particular task. Is there a simple solution using knitr/pandoc/linux command line I'm missing? Thanks.

推荐答案

2018年8月更新::此答案是在 bookdown 出现之前写的,该书是基于Rmarkdown的更强大的图书编写方法.在@ Mikey-Harper的 answer

August, 2018 update: This answer was written before the advent of bookdown, which is a more powerful approach to writing Rmarkdown based books. Check out the minimal bookdown example in @Mikey-Harper's answer!

当我想将一个大报表分解为单独的Rmd时,通常会创建一个父Rmd并将这些章节作为子章节.这种方法对于新用户来说很容易理解,并且如果您包含目录(toc),则在各章之间导航也很容易.

When I want to break a large report into separate Rmd, I usually create a parent Rmd and include the chapters as children. This approach is easy for new users to understand, and if you include a table of contents (toc), it is easy to navigate between chapters.

report.Rmd

---  
title: My Report  
output: 
  pdf_document:
    toc: yes 
---

```{r child = 'chapter1.Rmd'}
```

```{r child = 'chapter2.Rmd'}
```

chapter1.Rmd

# Chapter 1

This is chapter 1.

```{r}
1
```

chapter2.Rmd

# Chapter 2

This is chapter 2.

```{r}
2
```

构建

rmarkdown::render('report.Rmd')

哪个生产:

如果您想要一种快速的方法来创建子文档的块,则:

And if you want a quick way to create the chunks for your child documents:

rmd <- list.files(pattern = '*.Rmd', recursive = T)
chunks <- paste0("```{r child = '", rmd, "'}\n```\n")
cat(chunks, sep = '\n')
# ```{r child = 'chapter1.Rmd'}
# ```
#
# ```{r child = 'chapter2.Rmd'}
# ```

这篇关于如何将两个RMarkdown(.Rmd)文件合并到一个输出中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆