R:data.table函数在包中不起作用

时间:2018-08-21 11:19:44

标签: r data.table package

我正在尝试在基于mypackage的程序包(data.table)中构建一些自定义函数。我的文件如下:

DESCRIPTION

(...)
Imports:
    data.table, magrittr, glue, png, jpeg

R/dataWranglingCleaning.R

#' @title Numerify
#' @description Try to convert each column of a data.table `X` to numeric (except logicals).
#' @param X Data.table
#' @return NULL
#' @examples NULL
#' @export numerify
numerify <- function(X) {
    tryNumeric <- function(y) {
        if (!is.logical(y)) tryCatch(expr = {as.numeric(y)}, warning = function(cond) y) else y
    }
    X[, (names(X)) := lapply(.SD, tryNumeric)]
}

测试:

library(data.table)
library(mypackage)

X <- data.table(a = paste0(1:10),
                b = sample(c(T, F), 10, replace = T))

numerify(X)

Error in `:=`((names(X)), lapply(.SD, tryNumeric)) : 
  Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").

但是,如果我只是复制函数本身并在控制台中运行它,那么一切都可以正常工作,但是从程序包中加载后将停止工作。知道有什么问题吗?

0 个答案:

没有答案