簡體   English   中英

如何使用bit64功能將data.table :: fread包裝在您自己的包中?

[英]How to wrap data.table::fread in your own package, with bit64 capability?

我有一個包含一個從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")

我得到以下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

文檔建議用requireNamespace替換library 這將檢查包是否存在,但不會將其加載到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 ; 我只是看不出我錯過了什么。

我怎樣才能做到這一點?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM