[英]How to automatically load functions into namespace of an R package
I have an R package that requires functions from several other packages to be in the namespace.我有一个 R 包,它需要来自其他几个包的函数在命名空间中。 Using roxygen2 documentation, I've successfully installed these packages (ie
install.packages(dplyr)
), but I am unable to load them in automatically (ie library(dplyr)
).使用 roxygen2 文档,我已经成功安装了这些包(即
install.packages(dplyr)
),但我无法自动加载它们(即library(dplyr)
)。
Here is my DESCRIPTION file:这是我的描述文件:
Package: pkgname
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", email = "email@example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.5.2)
Imports:
ggplot2,
zoo,
tidyr,
dplyr,
magrittr
Suggests:
RColorBrewer
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
At the end of the documentation for one of my functions that requires the pipe function from magrittr, I included the following notation:在需要来自 magrittr 的管道函数的我的一个函数的文档末尾,我包含了以下符号:
#' @importFrom magrittr %>%
#'
#' @export
funName <- function(...) {
...
}
And the other function:另一个功能:
#' @import ggplot2
#' @importFrom magrittr %>%
#'
#' @export
funName2 <- function(...) {
...
}
And this successfully shows up in my NAMESPACE file:这成功地显示在我的 NAMESPACE 文件中:
# Generated by roxygen2: do not edit by hand
export(funName2)
export(funName)
import(ggplot2)
importFrom(magrittr,"%>%")
However, despite the added notation in roxygen2 comments and the correct script in my NAMESPACE file, I still have to load the packages ggplot2
and magrittr
using library(package-name)
at every new R session.然而,尽管在 roxygen2 注释中添加了符号并且我的 NAMESPACE 文件中的脚本正确,我仍然必须在每个新的 R 会话中使用
library(package-name)
加载包ggplot2
和magrittr
。 I expect this requirement for dplyr
, tidyr
and zoo
(since I do not explicitly load these, just install them), but I did not for ggplot2
or the %>%
operator.我希望
dplyr
、 tidyr
和zoo
这个要求(因为我没有明确加载这些,只需安装它们),但我没有ggplot2
或%>%
运算符。 Am I importing them incorrectly?我是否错误地导入了它们?
You should never use library(package.name)
inside your package functions.你永远不应该在你的包函数中使用
library(package.name)
。 Instead use package.name::function.name()
.而是使用
package.name::function.name()
。 You need to re-export the magrittr
pipe operator:您需要重新导出
magrittr
管道操作符:
1- put magrittr
into the DESCRIPTION file (as you did) 1- 将
magrittr
放入描述文件(如您所做的那样)
2- make an __imports.R
file to the packages R/
directory with the following lines: 2- 使用以下
__imports.R
文件创建到包R/
目录中:
#' re-export magrittr pipe operator
#'
#' @importFrom magrittr %>%
#' @name %>%
#' @rdname pipe
#' @export
NULL
Or, similarly do as Hadley Wickham says :或者,就像哈德利·威克姆所说的那样:
#' Pipe operator
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL
I think you can do the same for other imports when necessary.我认为你可以在必要时对其他进口做同样的事情。 Use
@importFrom
as much as you can, otherwise re-export it.尽可能多地使用
@importFrom
,否则重新导出它。
Try and run the following command to get your packages into NAMESPACE尝试运行以下命令将您的包放入 NAMESPACE
devtools::document()
After running it, all the imported (@import) packages are gonna be inserted into NAMESPACE.运行后,所有导入的 (@import) 包都将插入到 NAMESPACE 中。
Hope it helps ;)希望能帮助到你 ;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.