使用 knitr 编译 .Rmd 文件时调用第二个文件中的函数

Calling functions in a second file when compiling .Rmd files with knitr

我想用knitr格式化一个R markdown文件,我们就叫它Main.rmdMain.rmd 中的一些代码依赖于第二个文件中的辅助函数,我们称之为 Functions.rmd。 当我先 运行 Functions.rmd 然后 Main.rmd 时, Main.rmd 运行 中的代码很好。当我先运行Functions.rmd再尝试编织Main.rmd的时候,收到评价:

Error "Object 'myfunction' not found

如何在不将 Main.rmdFunctions.rmd 合并到一个文档中的情况下解决此问题,我想避免这样做?

编辑:我在下面添加了一个玩具示例。到目前为止,对于如何从 Main.rmd 调用 Functions.rmd 中的函数,有非常有用的建议,但它们都需要将 Functions.rmd 转换为 .R 文件。但是,对于我目前的目的,重要的是 Functions.rmd 也可以作为独立的降价文档来阅读。

首先,Main.rmd

---
title: "Main_test"
author: "Matt Nolan"
date: "25/06/2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Background.
This is the main body of text and code used to display results of analyses, some of which are created by calling functions in Functions.Rmd.

```{r cars}
myexamplefunction(1,2)
```

还有,这里是 Functions.rmd:

---
title: "Functions_test"
author: "Matt Nolan"
date: "25/06/2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Background
This is a document containing functions used in the document "Main_test". 
Because it contains functions and formatted text to explain the functions for an interested reader, it should be usable as a standalone markdown document.

For example, this is a function that adds two numbers.
```{r cars}
myexamplefunction <- function(a, b) {a + b}
```

30Jun2018 更新:R Markdown 不支持合并 Rmd 文件

Matt 的 25Jun2018 更新澄清了这个问题,询问如何将一个 Rmd 文档嵌入到另一个 Rmd 文档中。根据 R Markdown 网站,R Markdown 需要 一个 Rmd 文件。它目前不支持将一个 Rmd 文件嵌入到另一个 Rmd 文档中。

也就是说,使用 bookdown 包,您可以将 Rmd 文件构建为书中的章节,其中每个 Rmd 文件都是书中的一个章节。有关详细信息,请参阅 Bookdown:使用 R Markdown 1.4 - Two Rendering Approaches, the Getting Started page, and the Bookdown Demo github 库中内置的示例书籍的存储库 bookdown

2018 年 6 月 25 日更新:打印附录中的代码

根据 OP 的评论,将函数包含在 Rmd 文件而不是 R 文件中的原因是为了获得附录中代码的格式化打印输出。这可以通过我最初发布的技术加上一些更改来实现。

  1. 使用命名块将代码放在附录中,并使用参数echo=TRUEeval=FALSE避免多次执行。
  2. 通过ref.label=参数在文档的主流程中执行附录中的代码,并通过echo=FALSE参数使代码不在主文档中打印。
  3. 除了使用source()函数外,还必须在附录中的另一个块中打印每个函数,以获得每个函数的格式化打印。

下面列出了我的示例 Rmd 文件的更新版本。

---
title: "TestIncludedFiles"
author: "Len Greski"
date: "June 24, 2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Background

A question was posted on [Whosebug]( about how to include functions from one Rmd file while knitting another. 

If the second file contains R functions to be accessed in the second Rmd file, they're best included as R files rather than Rmd. In this example we'll include three files of functions from the Johns Hopkins University *R Programming* course: `pollutantmean()`, `corr()`, and `complete()`. We'll execute them in a subsequent code block. 

After an update to the original post where the original poster noted that he included the functions in an Rmd file in order to provide a formatted printout of the code in the report as an appendix, I've modified this example to account for this additional requirement. 

```{r ref.label="sourceCode",echo=FALSE}
 # execute sourceCode chunk from appendix 
```

## Executing the sourced files

Now that the required R functions have been sourced, we'll execute them.


```{r runCode, echo=TRUE}
pollutantmean("specdata","nitrate",70:72)
complete("specdata",1:10)
corr("specdata",threshold=500)
```

# Appendix 


```{r sourceCode,echo=FALSE,eval=FALSE}
# use source() function to source the functions we want to execute
source("./rprogramming/oneLine_pollutantmean.r")
source("./rprogramming/oneLine_complete.r")
source("./rprogramming/oneLine_corr.r")
```

The following is an inventory of the functions used in this Rmd file.

```{r }
pollutantmean
complete
corr
```

...以及文档附录部分的输出(为了避免发布 class 编程作业的答案而进行了编辑)。

原答案

如果第二个 Rmd 文件只包含函数,您最好将它们保存为 R 文件并使用 source() 将它们包含在 Main.Rmd 中。例如:

date: "June 24, 2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Background

A question was posted on [Whosebug]( about how to include functions from one Rmd file while knitting another. 

If the second file contains R functions to be accessed in the second Rmd file, they're best included as R files rather than Rmd. In this example we'll include three files of functions from the Johns Hopkins University *R Programming* course: `pollutantmean()`, `corr()`, and `complete()`. We'll execute them in a subsequent code block. 

```{r sourceCode,echo=TRUE}
# use source() function to source the functions we want to execute
source("./rprogramming/pollutantmean.r")
source("./rprogramming/complete.r")
source("./rprogramming/corr.r")
```

## Executing the sourced files

Now that the required R functions have been sourced, we'll execute them.


```{r runCode, echo=TRUE}
pollutantmean("specdata","nitrate",70:72)
complete("specdata",1:10)
corr("specdata",threshold=500)
```

...产生以下输出:

披露: 此答案包含我之前在 2016 年作为博客文章发布的技术 ToothGrowth Assignment: Accessing R Code from an Appendix in Knitr