R data.table breaks in exported functions

后端 未结 1 516
挽巷
挽巷 2020-12-03 13:46

I\'m having a problem getting data.table to work in roxygen2 exported functions.

Here\'s a simple, fake function in a file called foo.R (located in the R directory o

相关标签:
1条回答
  • 2020-12-03 14:09

    The issue, as @GSee pointed out (under comments) seems to be this issue still.

    In order to find out if a package is data.table aware, data.table calls the function cedta(), which is:

    > data.table:::cedta
    function (n = 2L) 
    {
        te = topenv(parent.frame(n))
        if (!isNamespace(te)) 
            return(TRUE)
        nsname = getNamespaceName(te)
        ans = nsname == "data.table" || "data.table" %chin% names(getNamespaceImports(te)) || 
            "data.table" %chin% tryCatch(get(".Depends", paste("package", 
                nsname, sep = ":"), inherits = FALSE), error = function(e) NULL) || 
            (nsname == "utils" && exists("debugger.look", parent.frame(n + 
                1L))) || nsname %chin% cedta.override || identical(TRUE, 
            tryCatch(get(".datatable.aware", asNamespace(nsname), 
                inherits = FALSE), error = function(e) NULL))
        if (!ans && getOption("datatable.verbose")) 
            cat("cedta decided '", nsname, "' wasn't data.table aware\n", 
                sep = "")
        ans
    }
    <bytecode: 0x7ff67b9ca190>
    <environment: namespace:data.table>
    

    The relevant check here is:

    "data.table" %chin% get(".Depends", paste("package", nsname, sep=":"), inherits=FALSE)
    

    When a package depends on data.table, the above command should return TRUE - that is, if you installed the package via R CMD INSTALL and then loaded the package. This is because, when you load the package, R by default creates a ".Depends" variable in the namespace as well. If you did:

    ls("package:test", all=TRUE)
    # [1] ".Depends" "foo"     
    

    However, when you do devtools:::load_all(), this variable doesn't seem to be set.

    # new session + set path to package's dir
    devtools:::load_all()
    ls("package:test", all=TRUE)
    # [1] "foo"
    

    So, cedta() doesn't get to know that this package indeed depends on data.table. However, when you manually set .datatable.aware=TRUE, the line:

    identical(TRUE, get(".datatable.aware", asNamespace(nsname), inherits = FALSE))
    

    gets executed, which will return TRUE and therefore overcomes the issue. But the fact that devtools doesn't place the .Depends variable in the package's namespace is still there.

    All in all, this is really not an issue with data.table.

    0 讨论(0)
提交回复
热议问题