仅加载一个函数的依赖项 [英] Load dependencies only for one function
问题描述
我正在创建一个包含一些功能的包.只有一个辅助功能需要plotly
.
I am creating a package with a few functions. Only one auxiliary function needs plotly
.
但是,当我使用 devtools
安装时,我得到一个注释 unused arguments in layout(yaxis = ay,...
然后我阅读了 Hadley 关于 imports
与 depends
的文章.使用 import 不会'不删除注释,但在 NAMESPACE 文件中添加 plotly
和 depends
可以解决问题.
However when I install using devtools
I get a note unused arguments in layout(yaxis = ay,...
Then I read Hadley's article about imports
vs depends
. Using import doesn't remove the note, but adding plotly
with depends
in the NAMESPACE-file solves the issue.
接下来我阅读了有关搜索路径"的段落.在这里,哈德利指出
Next I read the paragraph about 'Search Path'. Here Hadley states that
您永远不应该在包中使用 require() 或 library():相反,请使用 DESCRIPTION 中的 Depends 或 Imports 字段
You should never use require() or library() in a package: instead, use the Depends or Imports fields in the DESCRIPTION
我现在的问题是使用 plotly
的函数更像是包的附加组件.所有其他(更重要的)功能都与 base-R 一起使用.因此,我只想将 plotly
用于需要它的一个功能.
My issue now is that the function which uses plotly
is more of an add-on to the package. All other (more important) functions work with base-R.
Therefore I want to use plotly
only for the one function which needs it.
- 在
install
期间不创建注释是否可行? - 为什么
require
或library
在包中如此糟糕? - 可以先使用
requireNamespace
再使用require
吗?
- Is that possible without creating a note during
install
? - Why are
require
orlibrary
so bad inside a package? - Would it be okay to use
requireNamespace
and thenrequire
afterwards?
这里是一些示例代码:
#' Some plotly function
#'
#' Some very long description
#'
#' @param x_vec A numeric vector
#' @param y_vec A numeric vector
#' @keywords Some keywords
#' @return A plotly object
#' @export
#' @examples
debugMinEx<-function(x_vec,y_vec){
ay <- list(title = "",zeroline = FALSE,showline = FALSE,
showticklabels = FALSE, showgrid = FALSE,
scaleanchor="x",scaleratio=1) ## empty axis
ax <- list(title = "",zeroline = FALSE,showline = FALSE,
showticklabels = FALSE, showgrid = FALSE) ## empty axis
my_legend<-list(font = list(family = "sans-serif", size = 14, color = "#000000"),
x = 0, y = -0.05,orientation = "h")
plot_ly() %>%
add_trace(x=x_vec,y=y_vec,
type='scatter',mode='lines') %>%
layout(yaxis = ay,xaxis=ax,
legend = my_legend)
}
推荐答案
为此使用建议.
您可以在 编写 R 扩展 或 Hadley 的 包基础:说明.在这两种情况下,建议都是
You can read about it in Writing R Extensions or in Hadley's Package Basics: Decription. In both cases, the recommendation is
- 可选依赖项位于包
DESCRIPTION
的Suggests
字段中. - 在函数中使用
if (requireNamespace)
来测试它的包是否存在.
- The optional dependency goes in the
Suggests
field of your packageDESCRIPTION
. - Use
if (requireNamespace)
in the function to test it the package is there.
类似这样的:
if (requireNamespace("plotly", quietly = TRUE)) {
# do your plotly stuff
} else {
# do non-plotly stuff
# maybe as simple as stop("Please install plotly to use this function")
}
至于是否可以在 requireNamespace
之后使用 require
- 这似乎毫无意义.Hadley 的建议似乎很明确,使用 requireNamespace("plotly")
加载包,然后使用 plotly::
调用所需的函数.
As for whether you can use require
after requireNamespace
- that seems pointless. Hadley's recommendations seem to be pretty clear to use requireNamespace("plotly")
to load the package and subsequently use plotly::
to call the needed functions.
如果您愿意忽略此建议,那么只需在第一次执行 require
即可.使用 requireNamespace
后跟 require
是多余的.如您的链接中所述, requireNamespace
加载一个包而不附加它.require
加载和附加.
If you'd prefer to ignore this advice, then just do require
the first time. Using requireNamespace
followed by require
is redundant. As explained in your link, requireNamespace
loads a package without attaching it. require
both loads and attaches.
这篇关于仅加载一个函数的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!