使用 R Markdown 文档作为函数源 [英] Using an R Markdown Document as a source for functions

查看:38
本文介绍了使用 R Markdown 文档作为函数源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 R Markdown 以记录我经常使用的功能.我会将它们放入 R Markdown 文件中以记录它们,然后如果几个月后我再回来查看该函数背后的想法

I'm looking into R Markdown for documenting functions I regularly use. I will put them into an R Markdown file to document them and then be able to read my thinking behind the function if i come back to it months later

我的问题是,如果我开始一个新的 R 项目,是否可以获取 r markdown 文件并使用我创建的函数库,就像我正在获取常规 R 文件一样调用它们.我真的不想维护两套函数文件

My question is, if i start a new R project, Is it possible to source the r markdown file and use the library of functions i have created just by calling them similarly to if i was sourcing a regular R file. I dont really wish to maintain two sets of function files

我很欣赏这可能是一个初学者的问题,但任何指向教程等的帮助将不胜感激

I appreciate this may be a beginners question but any help pointing to tutorials and the like would be greatly appreciated

谢谢

推荐答案

正如评论中提到的,您可能应该为此目的创建一个包.但是,如果您坚持将函数定义放在脚本中并使用 RMarkdown 文件记录它们,那么使用 knitr 包中的 read_chunk() 可能是要走的路.

As was mentioned in the comments, you should probably create a package for this purpose. But if you insist on putting function definitions in scripts and document them using RMarkdown files, using read_chunk() from the knitr package might be the way to go.

请注意,此方法与您所要求的略有不同.您希望将 Markdown 文件中的函数定义与文档一起使用.然后你想以某种方式将该文件源到你的 R 脚本中以便使用该函数.我没有找到方法来做到这一点(即使有可能).

Note that this approach differs slightly from what you requested. You wanted to have the function definition in the markdown file together with the documentation. And then you wanted to somehow source that file into your R script in order to use the function. I did not find a way to do this (even though it might be possible).

我建议的替代方法是将函数定义放在它自己的 R 脚本中,比如 fun.R.然后 Rmarkdown 文件从 fun.R 读取函数定义并添加文档.如果你想在其他脚本中使用这个函数,你可以简单地使用 fun.R (而不是 Markdown 文件).这仍然意味着您只需维护一次函数定义的代码.

The alternative that I propose puts the function definition in its own R script, say fun.R. The Rmarkdown file then reads the function definition from fun.R and adds documentation. If you want to use the function in some other script, you can simply source fun.R (and not the markdown file). This still means that you have to maintain the code for the function definition only once.

让我用一个例子来说明这一点.这是fun.R:

So let me show this with an example. This is fun.R:

## ---- fun
fun <- function(x) x^2

第一行是后面会用到的标识符.降价文件如下:

The first line is an identifier that will be used later. The markdown file is as follows:

---
title: "Documentation of fun()"
output: html_document
---

This documents the function `fun()` defined in `fun.R`.
```{r,cache = FALSE}
knitr::read_chunk("fun.R")
```

This is the function definition
```{r fun}
```

This is an example of how  to use `fun()`:
```{r use_fun}
fun(3)
```

第一个块使用 knitr::read_chunk 读入 fun.R.稍后,您可以定义一个空块,其名称为 fun.R 中使用的标识符.这就像 fun.R 的内容直接写在这个文件中一样.如您所见,您还可以在后面的块中使用 fun() .这是生成的 html 文件的屏幕截图:

The first chunk reads in fun.R using knitr::read_chunk. Later on, you can define an empty chunk that has the identifier that was used in fun.R as its name. This will act as if the contents of fun.R were written directly in this file. As you see, you can also use fun() in later chunks. This is a screenshot of the resulting html file:

在您想要使用 fun() 的脚本中,您只需添加 source("fun.R") 来获取函数定义.

In a script where you want to use fun() you simply add source("fun.R") to source the function definition.

您也可以在一个 R 文件中包含多个函数,并且仍然单独记录它们.只需在每个函数定义之前放置一个以 ## ---- 开头的标识符,然后创建引用每个标识符的空块.

You could also have several functions in a single R file and still document them separately. Simply put an identifier starting with ## ---- before each function definition and then create empty chunks referring to each one of the identifiers.

无可否认,这比您要求的要复杂一些,因为它涉及两个文件,而不仅仅是一个.但至少没有冗余

This is admittedly somewhat more complicated than what you asked for, because it involves two files instead of just one. But at least there is no redundancy

这篇关于使用 R Markdown 文档作为函数源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆