我现在尝试了几个小时在 R 中构建一个包,并且对我的进步有多慢感到有点绝望。我很快就构建了一个没有依赖项的包,一切正常。由于几篇文章中的建议,我正在使用 R Studio、devtools 和 Roxygen2(在 Windows 上)。对于依赖项,我在检查时遇到问题(例如使用 devtools::check() ):
checking dependencies in R code ... NOTE Namespace in Imports field not imported from: 'ggplot2' All declared Imports should be used. See the information on DESCRIPTION files in the chapter 'Creating R packages' of the 'Writing R Extensions' manual.
此外, check() 删除
import(ggplot2)
命名空间中的行。如果我检查(document=F),它会给出一个关于 digest
的神秘错误。未加载的包。我阅读了 'Writing R Extensions' - 1.1.3 Package Dependencies 和 Hadley's Wiki,了解如何编写包,但无法解决我的问题。来自 CRAN 的其他 R 包的描述和命名空间文件看起来与我的不同(在我看来)?问题:我究竟做错了什么?对于这样一个基本的问题,我很抱歉,但我很茫然,到目前为止我看到的大多数分步教程在解释依赖关系之前都停止了。
到目前为止,我有 3 个文件:
说明:
Package: test
Type: Package
Title: Foo
Version: 1.0
Date: 2014-03-21
Author: Bar
Maintainer: Foo <bar@mail.com>
Description: Blubb
Imports:
ggplot2
License: GPL-3
命名空间:
export(is.equal.null)
import(ggplot2)
一个 R 文件:
#' Extension of compare to include NULLs
#'
#' Works as an extension to usual compare
#' Compares two basic objects which in addition to usual compare can be NULL
#' Intuitive output: TRUE if both are equal or NULL resp., FALSE if both are unequal or only one is NULL
#'
#' @param obj1 Basic object like \code{numeric, char, boolean, NULL}
#' @param obj2 Basic object like \code{numeric, char, boolean, NULL}
#' @keywords compare
#' @export
#' @examples
#' is.equal.null(5,5) # TRUE
#' is.equal.null(5,NULL) # FALSE
#' is.equal.null(NULL,NULL) # TRUE
is.equal.null <- function(obj1, obj2) {
# Small helper function to generalize comparison to comparison of NULL
# returns TRUE if both are NULL, and FALSE if only one of the objects is NULL
bool <- obj1==obj2
#qplot(obj1)
if (length(bool)) return(bool)
if (is.null(obj1) & is.null(obj2)) return(TRUE)
return(FALSE)
}
最佳答案
您需要在 中声明进口两个 地方:
Imports: ggplot2, pkg1, pkg2
import(ggplot2)
或避免命名空间冲突
importFrom(ggplot2, geom_point)
您可以使用
@import
获取 roxygen2 来维护 NAMESPACE 文件。和 @importFrom
标签。 在您的示例中,您的说明文件看起来不错,但您尚未将必要的功能添加到命名空间。
关于r - 如何在 R-package 中正确包含依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22565024/