在“建议”中提及“data.table”而不是“自定义包”的“导入”

时间:2015-07-20 18:29:04

标签: r package data.table

我正在编写一个R包,其中只有一小部分函数使用data.table中的函数。在Wickham's advice之后,我在DESCRIPTION文件的data.table字段中添加了Suggests:。我还使用data.table中的函数在每个函数的开头添加了if(! requireNamespace("data.table", quietly=TRUE))。 Moeover,每次,我使用data.table特定的函数,我先于data.table::

然而,我仍然遇到问题。由于data.table的FAQ仅处理DESCRIPTION文件的Depends:Imports:字段,这是否意味着Suggests不是一个选项?

这是导致问题的功能:

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)
}

以下是错误:

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

0 个答案:

没有答案