我有一个包含调用
下面是一个可重现的示例,在R 3.1.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
很顺利,但由于现在没有加载bit64
,fread
无法读取长整数。
尝试三
如果我将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
检查;我只是看不到我错过了什么。
我该怎么做?