【问题标题】:How to wrap data.table::fread in your own package, with bit64 capability?如何使用 bit64 功能将 data.table::fread 包装在您自己的包中?
【发布时间】:2015-03-10 10:49:26
【问题描述】:

我有一个包,其中包含一个从data.table 调用fread 的函数。 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

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

如果我将read_data 的定义更新为:

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

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


尝试 3

如果我将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

如果我在Depends 部分保留bit64,并使用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;我只是看不到我错过了什么。

我该怎么做?

【问题讨论】:

  • 您需要一个明确导入 bit64 的 NAMESPACE 文件(或者只是您想要从命名空间中导入的函数)。将 bit64 放在 Depends DESCRIPTION 字段中,添加 NAMESPACE 文件,这应该可以工作。见Writing R Extensions
  • @Thomas 谢谢。 roxygen2 正在为我创建 NAMESPACE 文件;它只是没有告诉它导入 bit64。

标签: r package data.table bit64


【解决方案1】:

尝试 3 最接近;我只需要在 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"
)

【讨论】:

    猜你喜欢
    • 2012-05-18
    • 2021-09-09
    • 1970-01-01
    • 2019-03-08
    • 2013-11-22
    • 1970-01-01
    • 2019-12-06
    相关资源
    最近更新 更多