仅加载一个功能的依赖项 [英] Load dependencies only for one function

查看:55
本文介绍了仅加载一个功能的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建具有一些功能的包装.只有一个辅助功能需要 plotly .

I am creating a package with a few functions. Only one auxiliary function needs plotly.

但是,当我使用 devtools 安装时,在布局中出现了未使用的参数的注释(yaxis = ay,... ),然后阅读了

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.

  1. 是否可以在 install 期间不创建注释?
  2. 为什么包中的 require library 这么糟糕?
  3. 可以先使用 requireNamespace 然后再使用 require 吗?
  1. Is that possible without creating a note during install?
  2. Why are require or library so bad inside a package?
  3. Would it be okay to use requireNamespace and then require 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 package DESCRIPTION.
  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆