我想使用knitr
格式化R markdown文件,让我们称之为Main.rmd
。 Main.rmd
中的某些代码依赖于第二个文件中的辅助函数,我们将其称为Functions.rmd
。
当我第一次运行Functions.rmd
然后运行Main.rmd
时,Main.rmd
中的代码运行良好。当我第一次运行Functions.rmd
然后尝试编织Main.rmd
时,我收到一个评估:
错误“找不到对象'myfunction'
如何解决此问题而不将Main.rmd
和Functions.rmd
合并到一个文档中,我想避免这样做?
编辑:我在下面添加了一个玩具示例。到目前为止,关于如何从Functions.rmd
调用Main.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}
```
答案 0 :(得分:0)
Matt的2018年6月25日更新澄清了这个问题,询问如何将一个Rmd文档嵌入到另一个Rmd文档中。根据{{3}}网站,R Markdown需要单个 Rmd文件。它当前不支持将一个Rmd文件嵌入另一个Rmd文档中。
也就是说,使用bookdown
包,您可以将Rmd文件构造为书中的章节,其中每个Rmd文件都是书中的一章。有关详细信息,请参见 Bookdown:使用R Markdown编写书籍 ,1.4 - Two Rendering Approaches页和Getting Started github存储库,以获取{{1 }}。
根据OP中的注释,将功能包含在Rmd文件而不是R文件中的原因是为了获取附录中代码的格式化打印输出。我最初发布的技术加上一些更改是可以实现的。
bookdown
和echo=TRUE
避免多次执行。 eval=FALSE
自变量在文档主流程中执行附录中的代码,并使用ref.label=
自变量阻止代码在主文档中打印。echo=FALSE
函数之外,还必须在附录的另一块中打印每个函数,以获得每个函数的格式化打印。下面列出了示例Rmd文件的更新版本。
source()
...以及文档“附录”部分的输出(为避免发布类编程分配的答案而进行了删节)。
如果第二个Rmd文件仅包含函数,则最好将它们另存为R文件,并使用---
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 [Stackoverflow](https://stackoverflow.com/questions/51013924/calling-functions-in-a-second-file-when-compiling-rmd-files-with-knitr) 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
```
将它们包括在source()
中。例如:
Main.Rmd
...产生以下输出: