[英]R package development - function aliases
I'm developing an R package, and I'd like to set some function aliases, eg if I have defined a function named foo
, I'd like it to be available under bar
symbol too. 我正在开发一个R包,我想设置一些函数别名,例如,如果我已经定义了一个名为
foo
的函数,我希望它也可以在bar
符号下使用。 Note that I'm aware of @alias
tag, but that's not what I want. 请注意,我知道
@alias
标签,但这不是我想要的。 Should I create a new file (probably aliases.R
) and put all aliases there? 我应该创建一个新文件(可能是
aliases.R
)并将所有别名放在那里吗?
You could just define bar
when you define foo
. 你可以只定义
bar
,当你定义foo
。
foo <- bar <- function(x, y, z) {
# function body goes here
}
I found this answer because also ran into the problem where foo <- bar <- function(x)...
would fail to export bar
because I was using royxgen2
. 我找到了这个答案,因为还遇到了
foo <- bar <- function(x)...
无法导出bar
因为我使用的是royxgen2
。 I went straight to the royxgen2
source code and found their approach: 我直接找到了
royxgen2
源代码并找到了他们的方法:
#' Title
#'
#' @param x
#'
#' @return
#' @export
#'
#' @examples
#' foo("hello")
foo <- function(x) {
print(x)
}
#' @rdname foo
#' @examples bar("hello")
#' @export
bar <- foo
This will automatically do three things: 这将自动完成三件事:
bar
as an alias of foo
(so no need to use @alias
tag). bar
作为foo
的别名(因此不需要使用@alias
标记)。 bar
to the Usage section of ?foo
(so no need to add @usage
tag). bar
添加到?foo
的Usage部分(因此无需添加@usage
标签)。 @examples
(note the plural) for the alias, it will add the examples to ?foo
. @examples
(注意复数),它会将示例添加到?foo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.