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