Rob*_*ace 82 r pandoc knitr r-markdown
我在同一个文件夹中有两个文件:chapter1.Rmd和chapter2.Rmd,其中包含以下内容:
chapter1.Rmd
---
title: "Chapter 1"
output: pdf_document
---
## This is chapter 1. {#Chapter1}
Next up: [chapter 2](#Chapter2)
Run Code Online (Sandbox Code Playgroud)
chapter2.Rmd
---
title: "Chapter 2"
output: pdf_document
---
## This is chapter 2. {#Chapter2}
Previously: [chapter 1](#Chapter1)
Run Code Online (Sandbox Code Playgroud)
我如何编织这些以便它们组合成单个pdf输出?
当然,render(input = "chapter1.Rmd", output_format = "pdf_document")工作完美,但render(input = "chapter1.Rmd", input = "chapter2.Rmd", output_format = "pdf_document")没有.
我为什么要这样做?将巨型文档分解为逻辑文件.
我已经使用@hadley的bookdown软件包从.Rmd构建乳胶,但这对于这个特殊的任务来说似乎有些过分.有没有使用knitr/pandoc/linux命令行的简单解决方案我不见了?谢谢.
Eri*_*ric 120
2018年8月更新:这个答案是在bookdown出现之前写的,这是一种更有效的方法来编写基于Rmarkdown的书籍.查看@ Mikey-Harper 答案中的最小小册子示例!
当我想将一个大型报告分成单独的Rmd时,我通常会创建一个父Rmd并将这些章节作为子项包含在内.这种方法很容易让新用户理解,如果你包含一个目录(toc),很容易在章节之间导航.
report.Rmd
---
title: My Report
output:
pdf_document:
toc: yes
---
```{r child = 'chapter1.Rmd'}
```
```{r child = 'chapter2.Rmd'}
```
Run Code Online (Sandbox Code Playgroud)
chapter1.Rmd
# Chapter 1
This is chapter 1.
```{r}
1
```
Run Code Online (Sandbox Code Playgroud)
chapter2.Rmd
# Chapter 2
This is chapter 2.
```{r}
2
```
Run Code Online (Sandbox Code Playgroud)
建立
rmarkdown::render('report.Rmd')
Run Code Online (Sandbox Code Playgroud)
哪个产生:

如果您想快速创建子文档的块:
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'}
# ```
Run Code Online (Sandbox Code Playgroud)
Mic*_*per 18
我建议人们使用bookdown软件包从多个R Markdown文件创建报告.它增加了许多有用的功能,如交叉引用,这对于较长的文档非常有用.
调整@Eric中的示例,这是预订设置的最小示例.主要细节是必须调用主文件index.Rmd,并且必须包含额外的YAML行site: bookdown::bookdown_site:
index.Rmd
---
title: "A Minimal bookdown document"
site: bookdown::bookdown_site
output:
bookdown::pdf_document2:
toc: yes
---
Run Code Online (Sandbox Code Playgroud)
01-intro.Rmd:
# Chapter 1
This is chapter 1.
```{r}
1
```
Run Code Online (Sandbox Code Playgroud)
02-intro.Rmd:
# Chapter 2
This is chapter 2.
```{r}
2
```
Run Code Online (Sandbox Code Playgroud)
如果我们编织index.Rmd bookdown将按字母顺序合并同一目录中的所有文件(可以使用额外_bookdown.yml文件更改此行为).
一旦您熟悉了这个基本设置,就可以使用其他配置文件(即_bookdown.yml和)来轻松自定义bookdown文档和输出格式_output.yml
进一步阅读
- R Markdown:权威指南:第11章概述了预订
- 使用bookdown创作书籍可提供有关预订的综合指南,并建议您提供更多高级详细信息.
这对我有用:
Rmd_bind <-
function(dir = ".",
book_header = readLines(textConnection("---\ntitle: 'Title'\n---")))
{
old <- setwd(dir)
if(length(grep("book.Rmd", list.files())) > 0){
warning("book.Rmd already exists")
}
write(book_header, file = "book.Rmd", )
cfiles <- list.files(pattern = "*.Rmd", )
ttext <- NULL
for(i in 1:length(cfiles)){
text <- readLines(cfiles[i])
hspan <- grep("---", text)
text <- text[-c(hspan[1]:hspan[2])]
write(text, sep = "\n", file = "book.Rmd", append = T)
}
render("book.Rmd", output_format = "pdf_document")
setwd(old)
}
Run Code Online (Sandbox Code Playgroud)
想象一下,有一个更好的解决方案,并且在 rmarkdown 或 knitr 包中有这样的东西会很好。