代码之家  ›  专栏  ›  技术社区  ›  Dan

调试函数时会话中止

  •  0
  • Dan  · 技术社区  · 5 年前

    我有一个玩具功能, foo ,这只是给变量加5 x 我还有第二个功能, n_foo 这适用 foo a n 时间。其工作原理如下:

    # Load library
    library(data.table)
    
    # Dummy function
    foo <- function(x){
      x + 5
    }
    
    # Apply foo n times
    n_foo <- function(x, n){
      Reduce(function(a, b) foo(a), 1:n, init = x)
    }
    
    # Dummy data
    dt <- data.table(values = 1:10)
    
    # Run foo 5 times
    dt[, test := n_foo(.SD, 5)]
    
    # See results
    dt
    #>     values test
    #>  1:      1   26
    #>  2:      2   27
    #>  3:      3   28
    #>  4:      4   29
    #>  5:      5   30
    #>  6:      6   31
    #>  7:      7   32
    #>  8:      8   33
    #>  9:      9   34
    #> 10:     10   35
    

    太棒了现在,说有点不对劲,我想调试一下 n_foo ,我会拿出信托 debug 功能。

    警告:以下代码可能会使您的会话崩溃。

    # Load library
    library(data.table)
    
    # Dummy function
    foo <- function(x){
      x + 5
    }
    
    # Apply foo n times
    n_foo <- function(x, n){
      Reduce(function(a, b) foo(a), 1:n, init = x)
    }
    
    # Dummy data
    dt <- data.table(values = 1:10)
    
    debug(n_foo)
    
    # Run foo 5 times
    dt[, test := n_foo(.SD, 5)]
    
    # See results
    dt 
    

    生产,

    enter image description here

    奇怪的是,会议 如果使用以下命令运行此代码,则会崩溃 reprex 。为什么此代码会导致致命错误?


    编辑:

    事实证明,我只能在RStudio和 在CLI上。RStudio标签已相应添加。


    R version 4.0.0 (2020-04-24)
    Platform: x86_64-apple-darwin17.0 (64-bit)
    Running under: macOS Catalina 10.15.5
    
    Matrix products: default
    BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
    LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
    
    locale:
    [1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] data.table_1.12.8
    
    loaded via a namespace (and not attached):
    [1] compiler_4.0.0 tools_4.0.0   
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   Wimpel    5 年前

    没有撞车。。。但进入调试阶段。。。

    debugging in: n_foo(.SD, 5)
    debug at #1: {
        Reduce(function(a, b) foo(a), 1:n, init = x)
    }
    Browse[2]> 
    

    信息

    > sessionInfo()
    R version 4.0.2 (2020-06-22)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 19041)
    
    other attached packages:
    [1] data.table_1.12.8
    
    rstudio 1.3.959
    
        2
  •  0
  •   Dan    5 年前

    升级到RStudio v.1.3.959后,我无法再重现错误。