当 `function` 是中缀运算符时,如何在包内使用 `package::function()`?
How to use `package::function()` inside a package when `function` is an infix operator?
根据 H. Wickham 的书 R Packages,在 Package Metadata 一章中,关于如何添加包依赖性,Hadley 指出了明确引用外部函数的充分理由,使用语法 package::function()
.
Adding a package dependency here ensures that it’ll be installed. However, it does not mean that it will be attached along with your package (i.e., library(x)). The best practice is to explicitly refer to external functions using the syntax package::function(). This makes it very easy to identify which functions live outside of your package. This is especially useful when you read your code in the future.
来自 R Packages | Package metadata.
但是function
是中缀运算符怎么办呢?比如我好像做不到1:10 magrittr::"%>%" sqrt
?在这里采用函数风格会违背使用管道运算符的目的......不是吗?
我认为您只需要在那里使用 magrittr 的导入就可以了。但是如果你想要你可以通过做
来确保你的符号指的是你想要的那个
your_fun <- function(){
`%>%` <- magrittr::`%>%`
# now use %>% like you normally would
# and you can be sure it refers to the magrittr version.
return(42)
}
您不能使用 package::infix
它与中缀语法,但您可以使用标准语法的中缀运算符。例如:
"+"(1, 2)
# [1] 3
Hmisc::"%nin%"(1:3, 2)
# [1] TRUE FALSE TRUE
不幸的是,当包未加载时,这不适用于 magrittr
,可能是由于它的替换技巧。
## what a shame that this beauty doesn't work:
magrittr::"%>%"(1:5, mean)
# Error in pipes[[i]] : subscript out of bounds
library(magrittr)
"%>%"(1:5, mean)
# [1] 3
在包上下文中,我建议只导入 magrittr
,或者至少导入 "%>%"
.
您引用的来源足以解释此最佳实践背后的一些原因:
This makes it very easy to identify which functions live outside of your package. This is especially useful when you read your code in the future.
这似乎不适用于这种情况。您不太可能忘记 %>%
的来源或错误地认为它是在您的包中创建的。
根据 H. Wickham 的书 R Packages,在 Package Metadata 一章中,关于如何添加包依赖性,Hadley 指出了明确引用外部函数的充分理由,使用语法 package::function()
.
Adding a package dependency here ensures that it’ll be installed. However, it does not mean that it will be attached along with your package (i.e., library(x)). The best practice is to explicitly refer to external functions using the syntax package::function(). This makes it very easy to identify which functions live outside of your package. This is especially useful when you read your code in the future.
来自 R Packages | Package metadata.
但是function
是中缀运算符怎么办呢?比如我好像做不到1:10 magrittr::"%>%" sqrt
?在这里采用函数风格会违背使用管道运算符的目的......不是吗?
我认为您只需要在那里使用 magrittr 的导入就可以了。但是如果你想要你可以通过做
来确保你的符号指的是你想要的那个your_fun <- function(){
`%>%` <- magrittr::`%>%`
# now use %>% like you normally would
# and you can be sure it refers to the magrittr version.
return(42)
}
您不能使用 package::infix
它与中缀语法,但您可以使用标准语法的中缀运算符。例如:
"+"(1, 2)
# [1] 3
Hmisc::"%nin%"(1:3, 2)
# [1] TRUE FALSE TRUE
不幸的是,当包未加载时,这不适用于 magrittr
,可能是由于它的替换技巧。
## what a shame that this beauty doesn't work:
magrittr::"%>%"(1:5, mean)
# Error in pipes[[i]] : subscript out of bounds
library(magrittr)
"%>%"(1:5, mean)
# [1] 3
在包上下文中,我建议只导入 magrittr
,或者至少导入 "%>%"
.
您引用的来源足以解释此最佳实践背后的一些原因:
This makes it very easy to identify which functions live outside of your package. This is especially useful when you read your code in the future.
这似乎不适用于这种情况。您不太可能忘记 %>%
的来源或错误地认为它是在您的包中创建的。