使用roxygen2导出函数列表 [英] Export a list of functions with roxygen2

查看:161
本文介绍了使用roxygen2导出函数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题 - 我想将函数列表导出为R程序包的一部分,最好使用roxygen2。



,我想导出列表中的函数,而不是列表本身。例如,考虑作为闭包生成的函数列表,如下所示:

  addval<  -  1:100 
fns< - lapply(addval,function(y){force(y); function(x)x + y})
names(fns)< - paste0(add_,addval)

然后问题是绑定函数(使用 fns code>,例如)到包环境中,然后将它们包括在包的导出函数中。



将函数绑定到包环境可以做得容易;一种方式是

  for(nm in names(fns))assign(nm,fns [[nm]])

但是可以使用roxygen2标签导出函数 add_1



更多要点:我想让roxygen2继续为我管理 NAMESPACE 文件,并且不想写 export()调用,直接。我可以看到这样做的唯一方法是通过编写代码生成

 #'@export add_1 
NULL

#'@export add_2
NULL

#...

或更好

 #'@export 
add_1< - fns [ add_1]]

#'@export
add_2< - fns [[add_2]]

#...



(放弃上述for循环)。



em> roxygen2已经有一个相当于这样的样板生成的设施,或者我自己必须提供这个设施吗?



我想到这样的设施更简洁地表示为

 #'@exportObjects names(fns)
NULL

其中标签 @exportObjects 会将其argument解释为字符向量

解决方案

可以使用 @rawNamespace 标签在 roxygen2 中,这允许在 NAMESPACE 文件中使用任意语法,包括 exportPattern ()

 #'@rawNamespace exportPattern(^ add _。* $)


Problem — I want to export a list of functions as part of an R package, ideally using roxygen2.

To be more precise, I want to export the functions in the list, rather than the list itself. For example, consider a list of functions that are generated as closures, like so:

addval <- 1:100
fns <- lapply(addval, function(y) {force(y); function(x) x + y})
names(fns) <- paste0("add_", addval)

Then the problem is to bind the functions (using the same names in fns, for instance) to the package environment, and then include them among the exported functions of the package.

Binding the functions to the package environment can be done easily enough; one way would be

for (nm in names(fns)) assign(nm, fns[[nm]])

But is it then possible to use roxygen2 tags to export the functions add_1, add_2, etc.?

More to the point: I would like roxygen2 to continue managing the NAMESPACE file for me, and would prefer not to have to write export() calls, directly. The only way I can see doing that is by writing code to generate boilerplate like

#' @export add_1
NULL

#' @export add_2
NULL

# ...

or better

#' @export
add_1 <- fns[["add_1"]]

#' @export
add_2 <- fns[["add_2"]]

# ...

(and forgo the above for-loop).

Does roxygen2 already have a facility equivalent to such boilerplate generation, or would I have to provide this facility myself?

I have in mind such a facility being expressed more succinctly as

#' @exportObjects names(fns)
NULL

where the tag @exportObjects would interpret its "argument" as a character vector of names of objects to export.

解决方案

You can use the @rawNamespace tag in recent roxygen2, this allows using arbitrary syntax valid in a NAMESPACE file, including exportPattern():

#' @rawNamespace exportPattern("^add_.*$")

这篇关于使用roxygen2导出函数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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