如何在自己的包中使用bit64功能封装data.table::fread函数?

3
我有一个包含调用data.table中的fread函数的功能的程序包。在data.table的DESCRIPTION文件的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")

我得到以下的 注意

* 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

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

如果我更新read_data的定义为:

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

然后运行R CMD check很顺利,但由于现在没有加载bit64fread无法读取长整数。


尝试三

如果我将DESCRIPTION更改为使bit64出现在Depends部分(而不是Suggests),并且保持read_data与尝试2中的相同,或简化为

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检查;我只是看不到我错过了什么。

我该怎么做?


1
你需要一个NAMESPACE文件,其中明确导入了bit64(或者仅从该命名空间中导入所需的函数)。将bit64放在Depends DESCRIPTION字段中,加入NAMESPACE文件,这样就可以工作了。请参阅编写R扩展 - Thomas
@Thomas 谢谢。roxygen2 已经为我创建了 NAMESPACE 文件;只是它没有告诉它导入 bit64。 - Richie Cotton
1个回答

4

第三次尝试最接近成功;我只需要在roxygen文档中添加一个额外的@import bit64

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"
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接