首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >用针织器编译.Rmd文件时调用第二个文件中的函数

用针织器编译.Rmd文件时调用第二个文件中的函数
EN

Stack Overflow用户
提问于 2018-06-24 21:00:17
回答 1查看 2.4K关注 0票数 1

我想使用knitr格式化一个R标记文件,让我们称之为Main.rmdMain.rmd中的一些代码依赖于第二个文件中的助手函数,让我们称之为Functions.rmd。当我第一次运行Functions.rmd,然后运行Main.rmd时,Main.rmd中的代码运行良好。当我第一次运行Functions.rmd,然后尝试编织Main.rmd时,我会收到一个评估:

错误“对象'myfunction‘找不到

如果不将Main.rmdFunctions.rmd合并成一个文档(我希望避免这样做),我如何解决这个问题?

编辑:我在下面添加了一个玩具例子。到目前为止,对于如何从Functions.rmdMain.rmd调用函数有非常有用的建议,但它们都需要将Functions.rmd转换为.R文件。然而,就我目前的目的而言,重要的是Functions.rmd也可以被理解为一个独立的标记文档。

首先,Main.rmd

代码语言:javascript
代码运行次数:0
运行
复制
---
title: "Main_test"
author: "Matt Nolan"
date: "25/06/2018"
output: html_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo =真)

代码语言:javascript
代码运行次数:0
运行
复制
## 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)

代码语言:javascript
代码运行次数:0
运行
复制

还有,这是Functions.rmd

代码语言:javascript
代码运行次数:0
运行
复制
---
title: "Functions_test"
author: "Matt Nolan"
date: "25/06/2018"
output: html_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo =真)

代码语言:javascript
代码运行次数:0
运行
复制
## 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}

代码语言:javascript
代码运行次数:0
运行
复制
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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文件中的原因是为了在附录中获得代码的格式化打印输出。通过我最初发布的技术加上一些更改,这是可能的。

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

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

代码语言:javascript
代码运行次数:0
运行
复制
---
title: "TestIncludedFiles"
author: "Len Greski"
date: "June 24, 2018"
output: html_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo =真)

代码语言:javascript
代码运行次数:0
运行
复制
## 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块

代码语言:javascript
代码运行次数:0
运行
复制
## 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)

代码语言:javascript
代码运行次数:0
运行
复制
# Appendix 


```{r sourceCode,echo=FALSE,eval=FALSE}

使用source()函数来获取我们想要执行的函数

source("./rprogramming/oneLine_pollutantmean.r")

源(“./rprogramming/oneLine_plete.r”)

源(“./rprogramming/oneLine_corr.r”)

代码语言:javascript
代码运行次数:0
运行
复制
The following is an inventory of the functions used in this Rmd file.

```{r }

污染平均

完成

更正

代码语言:javascript
代码运行次数:0
运行
复制

...and文档附录部分的输出(通过编辑以避免发布类编程分配的答案)。

原始答案

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

代码语言:javascript
代码运行次数:0
运行
复制
date: "June 24, 2018"
output: html_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo =真)

代码语言:javascript
代码运行次数:0
运行
复制
## 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”)

代码语言:javascript
代码运行次数:0
运行
复制
## 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)

代码语言:javascript
代码运行次数:0
运行
复制

...produces输出如下:

previously:这个答案包含了我之前在2016年作为博客文章ToothGrowth分配:从Knitr中的附录访问R代码发布的技巧。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51013924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档