简体   繁体   English

R包开发 - 函数别名

[英]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: 这将自动完成三件事:

  1. Add bar as an alias of foo (so no need to use @alias tag). 添加bar作为foo的别名(因此不需要使用@alias标记)。
  2. Add bar to the Usage section of ?foo (so no need to add @usage tag). bar添加到?fooUsage部分(因此无需添加@usage标签)。
  3. If you provide @examples (note the plural) for the alias, it will add the examples to ?foo . 如果为别名提供@examples (注意复数),它会将示例添加到?foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM