我想使用knitr
格式化一个R标记文件,让我们称之为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 =真)
## 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 =真)
## 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 <- -函数(a,b) {a + b}
发布于 2018-06-24 21:24:11
2018年6月30日更新:r标记不支持组合Rmd文件
Matt在2018年6月25日的更新澄清了这个问题,询问如何将一个Rmd文档嵌入到另一个Rmd文档中。根据R标记网站,Rmd需要一个Rmd文件。它目前不支持在另一个Rmd文档中嵌入一个Rmd文件。
尽管如此,使用bookdown
包,您可以将Rmd文件构造为一本书中的章节,其中每个Rmd文件都是书中的一章。有关详细信息,请参阅Bookdown:使用R Markdown 1.4 -两种渲染方法编写书籍、快速入门页面和Bookdown演示 github存储库,以获得在bookdown
中构建的示例图书。
2018年6月25日更新:在附录中打印代码
根据OP的注释,将函数包含在Rmd文件而不是R文件中的原因是为了在附录中获得代码的格式化打印输出。通过我最初发布的技术加上一些更改,这是可能的。
echo=TRUE
和eval=FALSE
避免多次执行。ref.label=
参数从文档主流程的附录中执行代码,并避免使用echo=FALSE
参数在主文档中打印代码。source()
函数之外,还必须在附录中的另一个块中打印每个函数,以便获得每个函数的格式化打印。下面列出了我的示例Rmd文件的更新版本。
---
title: "TestIncludedFiles"
author: "Len Greski"
date: "June 24, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo =真)
## 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}
从附录执行sourceCode块
## Executing the sourced files
Now that the required R functions have been sourced, we'll execute them.
```{r runCode, echo=TRUE}
污染平均值(“规格数据”,“硝酸盐”,70:72)
完成(“特定数据”,1:10)
corr("specdata",threshold=500)
# Appendix
```{r sourceCode,echo=FALSE,eval=FALSE}
使用source()函数来获取我们想要执行的函数
source("./rprogramming/oneLine_pollutantmean.r")
源(“./rprogramming/oneLine_plete.r”)
源(“./rprogramming/oneLine_corr.r”)
The following is an inventory of the functions used in this Rmd file.
```{r }
污染平均
完成
更正
...and文档附录部分的输出(通过编辑以避免发布类编程分配的答案)。
原始答案
如果第二个Rmd文件只包含函数,则最好将它们保存为R文件,并使用source()
将它们包含在Main.Rmd
中。例如:
date: "June 24, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo =真)
## 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.
```{r sourceCode,echo=TRUE}
使用source()函数来获取我们想要执行的函数
来源(“./rprogramming/污染度.r”)
源(“./rprogramming/plete.r”)
来源(“./rprogramming/corr.r”)
## Executing the sourced files
Now that the required R functions have been sourced, we'll execute them.
```{r runCode, echo=TRUE}
污染平均值(“规格数据”,“硝酸盐”,70:72)
完成(“特定数据”,1:10)
corr("specdata",threshold=500)
...produces输出如下:
previously:这个答案包含了我之前在2016年作为博客文章ToothGrowth分配:从Knitr中的附录访问R代码发布的技巧。
https://stackoverflow.com/questions/51013924
复制相似问题