【问题标题】:How to install a package so I can step through its R code with debugger?如何安装一个包,以便我可以使用调试器单步执行它的 R 代码?
【发布时间】:2017-01-16 23:27:28
【问题描述】:

我想安装 DESeq2 包,以便我可以使用调试器单步执行它。

这个包的源代码可以通过 GitHub 获得,但我不清楚如何安装这个包,以便我可以在调试器中单步调试它的 R 代码。

有没有办法做到这一点?


顺便说一句,我尝试了this earlier thread 中提出的方法,但我一无所获:

> trace(DESeq2::plotPCA, browser, at=1)
> devnull <- DESeq2::plotPCA(rld, intgroup = "q", returnData = TRUE)
Tracing DESeq2::plotPCA(rld, intgroup = "q", returnData = TRUE) step 1 
Called from: eval(expr, envir, enclos)
Browse[1]> n
debug: `{`
Browse[2]> n
debug: standardGeneric("plotPCA")
Browse[2]> n
> 

(即,在上面的最后一个n 之后,我回到了顶级提示。)

如果我在顶级提示符下输入DESeq2::plotPCA,我得到的只是

> DESeq2::plotPCA
nonstandardGenericFunction for "plotPCA" defined from package "BiocGenerics"

function (object, ...) 
{
    standardGeneric("plotPCA")
}
<environment: 0x26bee20>
Methods may be defined for arguments: object
Use  showMethods("plotPCA")  for currently available ones.

我也尝试只获取定义 DESeq2::plotPCA 的源文件,但这失败了

Error in setMethod("plotDispEsts", signature(object = "DESeqDataSet"),  : 
  no existing definition for function ‘plotDispEsts’

很明显,在获取此文件之前需要进行一些设置。正是这种认识导致了这篇文章。

【问题讨论】:

  • 您可能还受益于使用debugonce 函数
  • 我也遇到过这个问题。我尝试使用此博客从源代码构建它:hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch。即使使用library("devtools"),然后使用install("DESeq2", keep_source=TRUE),我也无法弄清楚如何获得有用的调试安装。

标签: r debugging


【解决方案1】:

debug()signature= 参数一起使用,例如,

> showMethods("plotPCA")
Function: plotPCA (package BiocGenerics)
object="DESeqTransform"

> debug(plotPCA, signature="DESeqTransform")
Tracing specified method for function "plotPCA" in environment
<namespace:BiocGenerics>

无需特殊安装,只需BiocInstaller::biocLite("DESeq2")

【讨论】:

    【解决方案2】:

    Martin Morgan 的回答抓住了解决方案的精髓,但是 有几个 gotchya 让新 R 用户非常困惑。这些 gotchya 源于 R 使用的独特的面向对象形式,这对于那些来自 C/C++ 或 Python 背景的人来说是难以置信的困惑。

    使用我自己的数据集遵循 DESeq2 vignette

    $ cat synth.dat
    sample    g0     g1     g2     g3     g4     g5     g6     g7     g8     g9    
    samp0    132    192    19     133    247    297    110    104    93     103    
    samp1    173    152    23     139    245    307    83     77     76     123    
    samp2    179    129    18     130    208    244    89     138    71     142        
    samp3    178    145    22     157    323    277    79     93     102    97    
    samp4    250    208    8      101    202    257    142    140    76     113    
    samp5    221    157    12     79     261    341    140    94     56     123    
    samp6    139    220    15     125    282    261    124    154    117    118    
    samp7    213    121    16     115    377    322    117    154    57     81    
    samp8    234    152    11     103    281    321    76     160    71     139        
    samp9    254    120    13     134    323    207    122    122    82     91    
    samp10   159    207    17     143    385    217    126    113    106    89     
    samp11   214    136    14     90     364    365    149    102    93     111    
    samp12   180    159    15     136    226    309    72     111    69     113    
    samp13   151    137    17     122    229    297    131    108    112    70    
    samp14   254    151    8      118    254    222    138    114    66     89    
    samp15   275    121    13     105    238    408    122    156    57     72    
    samp16   204    134    8      111    352    332    89     134    73     90         
    samp17   265    144    11     144    211    281    134    98     71     114        
    samp18   212    111    14     138    321    391    84     112    88     96    
    samp19   155    164    12     119    174    380    129    106    66     86    
    
    $ cat synth_design_matrix.txt
    samp0  group0
    samp1  group0
    samp2  group0
    samp3  group0
    samp4  group0
    samp5  group0
    samp6  group0
    samp7  group0
    samp8  group0
    samp9  group0
    samp10  group1
    samp11  group1
    samp12  group1
    samp13  group1
    samp14  group1
    samp15  group1
    samp16  group1
    samp17  group1
    samp18  group1
    samp19  group1
    
    > library("DESeq2")
    > dat <- read.table(file="synth.dat", header=TRUE, stringsAsFactors=FALSE, row.names=1)
    > groups <- read.table(file="synth_design_matrix.txt", header=FALSE, stringsAsFactors=TRUE, row.names=1)
    > colnames(groups) <- c("condition")
    > datM <- t(as.matrix(dat))
    > dds  <- DESeqDataSetFromMatrix(countData = datM, colData = groups, design = ~condition)
    > dds$condition <-relevel(dds$condition, ref="group0")
    > vsd <- vst(dds, blind=FALSE, nsub=10)
    -- note: fitType='parametric', but the dispersion trend was not well captured by the
       function: y = a/x + b, and a local regression fit was automatically substituted.
       specify fitType='local' or 'mean' to avoid this message next time.
    

    现在指定跟踪点并逐步执行。

    > trace(what="plotPCA", tracer=browser, at=1, signature=c("DESeqTransform"))
    [1] "plotPCA"
    > plotPCA(vsd)
    Tracing function ".local" in package "DESeq2"
    Tracing .local(object, ...) step 1 
    Called from: eval(expr, p)
    Browse[1]> n
    debug: `{`
    Browse[2]> n
    debug at /tmp/RtmpaiGvIe/R.INSTALL5ef336529904/DESeq2/R/plots.R#184: rv <- rowVars(assay(object))
    Browse[2]> n
    debug at /tmp/RtmpaiGvIe/R.INSTALL5ef336529904/DESeq2/R/plots.R#187: select <- order(rv, decreasing = TRUE)[seq_len(min(ntop, length(rv)))]
    Browse[2]> n
    debug at /tmp/RtmpaiGvIe/R.INSTALL5ef336529904/DESeq2/R/plots.R#190: pca <- prcomp(t(assay(object)[select, ]))
    Browse[2]> c
    

    现在是gotchya的:

    1. 您只能在包中导出的函数/类上直接设置跟踪点。它们将包含#' @export。请参阅 Hillary Parker 的blog 了解简明详情。在此示例中,我们最终单步执行 plotPCA 方法并进入在 DESeq2 命名空间中不可见的 plotPCA.DESeqTransform 函数。

    2. 如果方法的签名有多个参数,则需要用 R 的c() 指定。

    例如如果方法原型是:

    setMethod("spin", signature(object="star", value="numeric"), function(object, value){some stuff here})
    

    跟踪点是

    trace(what="spin", tracer=browser, at=1, signature=c(object="star", value="numeric"))
    
    1. 注意替换方法。如果您不理解它们,那么在调试像 DESeq2(有多个)这样的包时可能会非常混乱。请参阅herehere 了解更多详情。

    2. 熟悉S4S3 方法以及R 的object orientation。这将更容易理解包中发生的事情。

    使用这些工具,您应该能够调试任何下载的 R 包 CRAN 或 Bioconductor 没有任何特殊的安装说明。

    【讨论】:

      猜你喜欢
      • 2010-11-07
      • 2017-04-04
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2018-12-26
      • 2019-09-08
      • 1970-01-01
      相关资源
      最近更新 更多