3

我有一个包,其中包含一个freaddata.table. data.table在其说明文件的 Suggests 字段中有bit64包,它提供fread了将大整数导入为 integer64而不是numeric. 我的包中默认需要此功能。

这是一个可重现的示例,在 R 3.1.3 下(早期版本没有这个问题)。


尝试 1

Vectorize(dir.create)(c("test", "test/R", "test/man"))

cat(
  "Package: test
Title: Test pkg
Description: Investigate how to use suggested package
Version: 0.0-1
Date: 2015-03-10
Author: Richie Cotton
Maintainer: Richie Cotton <a@b.com>
Imports: data.table
Suggests: bit64
License: Unlimited
",
  file = "test/DESCRIPTION"
)

cat(
  "#' Read data
#' 
#' Wrapper to \\code{fread} that loads bit64 first
#' @param ... Passed to fread.
#' @return A data frame of uniformly distributed random numbers and their index.
#' @importFrom data.table fread
#' @export
read_data <- function(...)
{
  library(bit64)
  fread(...)
}",
  file = "test/R/read_data.R"
)

当我跑步时R CMD check

library(roxygen2)
library(devtools)
roxygenize("test")
check("test")

我得到以下信息NOTE

* checking dependencies in R code ... NOTE
'library' or 'require' call to 'bit64' in package code.
Please use :: or requireNamespace() instead.
See section 'Suggested packages' in the 'Writing R Extensions' manual.

尝试 2

文档建议替换libraryrequireNamespace. 这会检查包是否存在,但不会将其加载到 R 的搜索路径中。

如果我将定义更新read_data为:

read_data <- function(...)
{
  if(!requireNamespace('bit64')) 
  {
    warning('bit64 not available.')
  }
  fread(...)
}

然后R CMD check运行顺利,但由于bit64现在没有加载,fread没有读取长整数的能力。


尝试 3

如果我更改部分中的so DESCRIPTION(而不是, 并保持尝试 2,或将其简化为bit64DependsSuggestsread_data

read_data <- function(...)
{
  fread(...)
}

然后R CMD check给出NOTE

* checking dependencies in R code ... NOTE
Package in Depends field not imported from: 'bit64'
  These packages need to be imported from (in the NAMESPACE file)
  for when this namespace is loaded but not attached.

我不太确定在这种情况下我应该导入什么。


尝试 4

如果我保留bit64在该Depends部分中,并使用 的原始定义read_data

read_data <- function(...)
{
  library(bit64)
  fread(...)
}

然后R CMD check给出NOTE

* checking dependencies in R code ... NOTE
'library' or 'require' call to 'bit64' which was already attached by Depends.
Please remove these calls from your code.
Package in Depends field not imported from: 'bit64'

我觉得应该有一些神奇的DESCRIPTION函数定义组合,为我提供bit64功能并R CMD check干净利落地通过;我只是看不到我错过了什么。

我怎样才能做到这一点?

4

1 回答 1

4

尝试 3 最接近;@import bit64我只需要在 roxygen 文档中添加额外内容。

Vectorize(dir.create)(c("test", "test/R", "test/man"))

cat(
  "Package: test
Title: Test pkg
Description: Investigate how to use suggested package
Version: 0.0-1
Date: 2015-03-10
Author: Richie Cotton
Maintainer: Richie Cotton <a@b.com>
Depends: bit64
Imports: data.table
License: Unlimited
",
  file = "test/DESCRIPTION"
)

cat(
  "#' Read data
#' 
#' Wrapper to \\code{fread} that loads bit64 first
#' @param ... Passed to fread.
#' @return A data frame of uniformly distributed random numbers and their index.
#' @import bit64
#' @importFrom data.table fread
#' @export
read_data <- function(...)
{
  fread(...)
}",
  file = "test/R/read_data.R"
)
于 2015-03-10T11:55:23.420 回答