gpt4 book ai didi

r - 如何将两个 RMarkdown (.Rmd) 文件合并为一个输出?

转载 作者:行者123 更新时间:2023-12-03 05:08:08 24 4
gpt4 key购买 nike

我在同一文件夹中有两个文件:chapter1.Rmd和chapter2.Rmd,其内容如下:

第1章.Rmd

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

## This is chapter 1. {#Chapter1}

Next up: [chapter 2](#Chapter2)

第2章.Rmd

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

## This is chapter 2. {#Chapter2}

Previously: [chapter 1](#Chapter1)

如何将它们组合成一个 pdf 输出?

当然,render(input = "chapter1.Rmd", output_format = "pdf_document") 工作得很好,但是render(input = "chapter1.Rmd", input = "chapter2. Rmd", output_format = "pdf_document") 没有。

我为什么要这样做?分手giant document到逻辑文件中。

我用过@hadley的bookdown包从 .Rmd 构建 latex ,但这对于这个特定任务来说似乎有点矫枉过正。有没有使用我缺少的 knit/pandoc/linux 命令行的简单解决方案?谢谢。

最佳答案

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),则可以轻松在章节之间导航。

报告.Rmd

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

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

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

第1章.Rmd

# Chapter 1

This is chapter 1.

```{r}
1
```

第2章.Rmd

# Chapter 2

This is chapter 2.

```{r}
2
```

构建

rmarkdown::render('report.Rmd')

它产生: My report

如果您想要一种快速的方法来为您的子文档创建 block :

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'}
# ```

关于r - 如何将两个 RMarkdown (.Rmd) 文件合并为一个输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com