3

I have a package containing a function that calls fread from data.table. data.table has the bit64 package in the Suggests field of its DESCRIPTION file, which gives fread the capability to import large integers as integer64 rather than numeric. I need this capability by default in my package.

Here's a reproducible example, under R 3.1.3 (earlier versions don't have this issue).


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

When I run R CMD check,

library(roxygen2)
library(devtools)
roxygenize("test")
check("test")

I get the following 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.

Attempt 2

The documentation suggests replacing library with requireNamespace. This checks to see if the package exists, but doesn't load it onto R's search path.

If I update the definition of read_data to:

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

then R CMD check runs smoothly, but since bit64 is now not loaded, fread doesn't have the ability to read long integers.


Attempt 3

If I change the DESCRIPTION so that bit64 is in the Depends section (instead of Suggests, and keep read_data as in attempt 2, or simplify it to

read_data <- function(...)
{
  fread(...)
}

then R CMD check gives the 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.

I'm not quite sure what I should be importing in this case.


Attempt 4

If I keep bit64 in the Depends section, and use the original definition of read_data,

read_data <- function(...)
{
  library(bit64)
  fread(...)
}

then R CMD check gives the 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'

I feel like there should be some magic combination of DESCRIPTION and function definitions that gives me the bit64 functionality and passes R CMD check cleanly; I just can't see what I've missed.

How can I do this?

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • 1
    You need a NAMESPACE file that explicitly imports bit64 (or just the functions you want from the namespace). Put bit64 in the Depends DESCRIPTION field, add the NAMESPACE file, and this should work. See [Writing R Extensions](http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Specifying-imports-and-exports). – Thomas Mar 10 '15 at 11:37
  • @Thomas Thanks. roxygen2 was creating the NAMESPACE file for me; it just hadn't told it to import bit64. – Richie Cotton Mar 10 '15 at 12:00

1 Answers1

4

Attempt 3 was closest; I just needed an extra @import bit64 in the roxygen documentation.

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"
)
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360