Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R package development - function aliases

Tags:

package

r

I'm developing an R package, and I'd like to set some function aliases, e.g. if I have defined a function named foo, I'd like it to be available under bar symbol too. Note that I'm aware of @alias tag, but that's not what I want. Should I create a new file (probably aliases.R) and put all aliases there?

like image 702
aL3xa Avatar asked Jan 30 '12 22:01

aL3xa


2 Answers

You could just define bar when you define foo.

foo <- bar <- function(x, y, z) {
  # function body goes here
}
like image 107
Joshua Ulrich Avatar answered Nov 18 '22 02:11

Joshua Ulrich


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. I went straight to the royxgen2 source code and found their approach:

#' 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).
  2. Add bar to the Usage section of ?foo (so no need to add @usage tag).
  3. If you provide @examples (note the plural) for the alias, it will add the examples to ?foo.
like image 45
DuckPyjamas Avatar answered Nov 18 '22 04:11

DuckPyjamas