![](/img/trans.png)
[英]R, bit64, problems calculating row mean and standard deviation in data.table
[英]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下(早期版本沒有這個問題)。
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.
文檔建議用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.
在這種情況下,我不太確定我應該導入什么。
如果我在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.