4

I'm writing a package that contains a reading function that needs to read date columns in a specific format. I'm using data.table, and a SO question suggests using the methods::setClass() and methods::setAs() functions to deal with this case.

So far so good, it works fine. But when I check the package to submit it to CRAN I get the following NOTE:

NOTE: Namespaces in Imports field not imported from: 'methods'

Apparently this is because I'm creating a build-time dependency on methods, not a run-time dependency. Hadley here suggests just adding a @importFrom methods setClass setAs line, which doesn't seem very elegant, but does the trick.

Since this Google Groups discussion I linked is fairly old by now, and this is the first time I have to use these functions in a package I'm writing, I've been wondering if there is a different recommendation on how to deal with these methods::setClass() inside packages nowadays.

Check below for a bit more detail on how I'm currently using these functions in my code:

#' Set class and method to read dates as formatted in GTFS to a 'Date' object
#'
#' This is a build-time dependency on methods, as opposed to a run-time
#' dependency, thus requiring the importFrom tag to avoid a NOTE when checking
#' the package on CRAN.
#'
#' @keywords internal
#' @importFrom methods setClass setAs
methods::setClass("gtfs_date")
methods::setAs(
  "character",
  "gtfs_date",
  function(from) as.Date(from, format = "%Y%m%d")
)
dhersz
  • 525
  • 2
  • 8

0 Answers0