2

I am writing an R package where only a small subset of the functions use functions from data.table. Following Wickham's advice, I added data.table in the Suggests: field of the DESCRIPTION file. I also added if(! requireNamespace("data.table", quietly=TRUE)) at the beginning of each of my functions using a function from data.table. Moeover, each time, I use a data.table-specific function, I precede it with data.table::.

However, I am still encountering problems. As the FAQ of data.table only deals with the Depends: and Imports: fields of the DESCRIPTION file, does it mean that Suggests isn't an option?

Here is a function causing problems:

depths.per.sample <- function(dat, min.reg.len=30, max.reg.len=500,
                              min.reg.dep=10, max.reg.dep=100,
                              min.reg.frac=0.25){
  if(! requireNamespace("data.table", quietly=TRUE))
    stop("Pkg needed for this function to work. Please install it.",
         call.=FALSE)
  stopifnot(data.table::is.data.table(dat))
  for(col in c("ind", "flowcell", "lane", "start", "end", "depth", "fraction"))
    stopifnot(col %in% colnames(dat))

  ## http://stackoverflow.com/a/8096882/597069
  depth=fraction=chrom=ind=flowcell=lane=NULL

  data.table::setkey(dat, NULL)
  data.table::setkeyv(x=dat, cols=c("ind", "flowcell", "lane"))

  depths.sample <- dat[end - start >= min.reg.len &
                         end - start <= max.reg.len,
                       list(depth.len=data.table::.N,
                            depth.min=min(data.table::.SD[,depth]),
                            depth.med=as.double(median(data.table::.SD[,depth])),
                            depth.mean=mean(data.table::.SD[,depth]),
                            depth.max=max(data.table::.SD[,depth]),
                            depth.q65=quantile(data.table::.SD[,depth], 0.65),
                            depth.q70=quantile(data.table::.SD[,depth], 0.70),
                            depth.q75=quantile(data.table::.SD[,depth], 0.75),
                            depth.q80=quantile(data.table::.SD[,depth], 0.80),
                            reg.ok=nrow(unique(data.table::.SD[depth >= min.reg.dep &
                                                                 depth <= max.reg.dep &
                                                                   fraction >= min.reg.frac,
                                list(chrom,start,end)]))),
                       by=list(ind,flowcell,lane)]

  return(depths.sample)
}

And here are the errors:

Error in x[j] : invalid subscript type 'list'
In addition: Warning messages:
1: In min(data.table::.SD[, depth]) :
  no non-missing arguments to min; returning Inf
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
3: In mean.default(data.table::.SD[, depth]) :
  argument is not numeric or logical: returning NA
4: In max(data.table::.SD[, depth]) :
  no non-missing arguments to max; returning -Inf
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
tflutre
  • 3,354
  • 9
  • 39
  • 53
  • 3
    In my experience, `Imports:` is the way to go, making sure to create the NAMESPACE file correctly. You won't need to write the extra code into the function(s) either. – Rich Scriven Jul 20 '15 at 18:34
  • *suggests* should works IMO, do you have it on github maybe? you can also try `require` instead `requireNamespace`, still using *suggests*, then of course you don't need `data.table::` prefix. `requireNamespace` is not attaching the package and that can be the case. Also `data.table::.SD` looks suspicious. – jangorecki Jul 20 '15 at 18:35
  • 2
    `Import` data.table. I find the `::` totally unnecessary. `.SD` is a symbol, don't use `::` on it, for sure. If necessary, just import the functions you use in your package, but your package *requires* data.table, and is definitely not *suggesting*. Not sure how you got that reading that link. – Arun Jul 20 '15 at 18:53
  • 1
    @Arun ok, I'll `Import`. Here is what I read: "`Suggests`: your package can use these packages, but doesn’t require them. You might use suggested packages for example datasets, to run tests, build vignettes, or maybe there’s only one function that needs the package.". – tflutre Jul 20 '15 at 19:23

0 Answers0