简体   繁体   English

如何制作 function 别名?

[英]How to make function aliases?

I am trying to create aliases for functions in R.我正在尝试为 R 中的函数创建别名。

For example, to get the length of a vector in R:例如,要获取 R 中向量的长度:

length(the_vector)
#returns the length of the vector

I want to create an alias of the function called "len":我想创建一个名为“len”的 function 的别名:

len(the_vector)
#will also return the length of the vector

Is there a way to do this?有没有办法做到这一点? Key points that I want is that I want the alias to be able to take all arguments that could be taken by the original function, and also that the alias does not replace the original function. (ie in the example above, calling either length(the_vector) or len(the_vector) would provide the same result?我想要的关键点是我希望别名能够采用原始 function 可以采用的所有arguments,并且别名不会替换原始 function。(即在上面的示例中,调用length(the_vector)len(the_vector)会提供相同的结果吗?

The comment from @russ-hyde provides the basis for an answer. @russ-hyde 的评论提供了答案的基础。 Including:包括:

len <- base::length

in your .R file creates a variable that can be used in every way that base::length can be used.在您的.R文件中创建一个变量,该变量可用于base::length的所有使用方式。

As long as the "alias definition" is always run as part of the source file that uses it used, this does not make it less portable.只要“别名定义”始终作为使用它的源文件的一部分运行,这不会降低它的可移植性。 There is a case that this reduces legibility of your code, however, since collaborators may not know what to use it for.但是,在某些情况下,这会降低代码的可读性,因为合作者可能不知道将其用于什么目的。

There is also the possibility that defining len will cause a name conflict.定义len也有可能导致名称冲突。 But on the other hand, you might be doing this to avoid a name conflict – if, for example, you have loaded a package that defines a length() function you could use len() for the standard one.但另一方面,您这样做可能是为了避免名称冲突——例如,如果您加载了一个 package,它定义了一个length() function,您可以将len()用于标准的。 Not that in this case the base:: prefix becomes very important to disambiguate between the multiple objects you might want to be aliasing for.并不是说在这种情况下base::前缀对于消除您可能想要为其别名的多个对象之间的歧义变得非常重要。

For example, dplyr overloads the filter function and is widely used.例如, dplyr使filter function 过载并被广泛使用。 So if you need to use the original filter function for time series, you might want to define:因此,如果您需要将原始filter function 用于时间序列,您可能需要定义:

tsfilter <- stats::filter

To not have to modify each file you're using, you can add your alias to your .Rprofile to be loaded automatically.为了不必修改您正在使用的每个文件,您可以将您的别名添加到您的.Rprofile以自动加载。 I have muscle memory for using exit to quit a repl instead of quit() , so my .Rprofile includes我有肌肉 memory 用于使用exit退出 repl 而不是quit() ,所以我的.Rprofile包括

print.command <- function (cmd) {
  default.args <- attr(cmd, "default.args")
  if (length(default.args) == 0L) default.args <- list()
  res <- do.call(cmd, default.args, envir = parent.frame(2))
  if (attr(cmd, "print_result")) print(res)
  invisible(NULL)
}

make_command <- function(x, ..., print = TRUE) {
  class(x) <- c("command", class(x))
  attr(x, "default.args") <- list(...)
  attr(x, "print_result") <- print
  x
}

exit <- make_command(quit, print = FALSE)

With code modified from here这里修改代码

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

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