[英]Use explicit namespace with infix operator
The dplyr
R package has the %>%
operator, which is a custom infix operator. dplyr
R软件包具有%>%
运算符,它是一个自定义的中缀运算符。 If one attaches the namespace with library(dplyr)
one can use this operator. 如果将名称空间附加到
library(dplyr)
则可以使用此运算符。 In library code the library(dplyr)
at the top of the file has no effect because the environment after executing the source code is stored; 在库代码中,文件顶部的
library(dplyr)
不起作用,因为存储了执行源代码后的环境; loaded packages have no effect on that. 加载的软件包对此没有影响。
So in order to use this in my library, I have these options: 因此,为了在我的库中使用它,我有以下选择:
library(dplyr)
at the beginning of each function. library(dplyr)
。 %>%
. %>%
编写函数。 dplyr::%>%
. dplyr::%>%
。 The last option is what I want to do, but I cannot seem to get the syntax right. 最后一个选项是我想做的,但是我似乎无法正确理解语法。 I have tried
我努力了
dplyr::%>%
and get parsing errors. 并获得解析错误。 Also
也
dplyr::`%>%`
does not work. 不起作用。 And
和
`dplyr::%>%`
does not work either. 也不起作用。 I don't think that there is any other way to place the backticks.
我认为没有其他方法可以放置反引号。 Is this something that is possible in R or do I just have to use option 1 or 2?
这在R中是可能的,还是我只需要使用选项1或2?
Just import the pipe operator, by adding a line like 只需导入管道操作符,只需添加如下一行即可
importFrom(magrittr, "%>%")
in your NAMESPACE
file, or if you're using roxygen2
, putting 在您的
NAMESPACE
文件中,或者如果您使用的是roxygen2
,
#' @importFrom magrittr %>%
into one of your .R
files to do the same thing. 放入您的
.R
文件之一即可执行相同操作。
You may or may not want to export it as well. 您可能也可能不想将其导出。 Do export it with a line like
用以下行导出它
export("%>%")
in your NAMESPACE
file or with roxygen2
在您的
NAMESPACE
文件中或使用roxygen2
#' @export
magrittr::`%>%`
if you want your users to use the pipe operator when they are using your package. 如果希望用户在使用软件包时使用管道运算符。 Don't export it if you only need it to be available internally.
如果只需要内部可用,则不要导出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.