问题描述
我在同一文件夹中有两个文件: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.
推荐答案
当我想将一个大报表分解为单独的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)文件合并到一个输出中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!