如何在r包函数中@importFrom data.table

时间:2017-11-06 17:12:04

标签: r

我正在开发一个包中的函数。我不认为输入数据是相关的,所以我没有发布它。我试图只根据建议将包中的特定函数导入到这个函数中,除了data.table之外,这几乎可以正常工作。

#' @importFrom data.table 'setDT' 'rowid' '.SD'
 #' @keywords Sankey
 #' @export
    SurveySankey <- function(dfw, y,PatientID) {
      # Create the Sankey diagrams
      Sankey <-
        dcast(data.table::setDT(dfw)[, .SD, PatientID], 
              PatientID ~ rowid(PatientID),
              value.var = y)
    }

如果我这样做,我会收到错误:

1. Error: SurveySankey (@test.R#400) -------------------------------------------------------------------------------------------------
object '.SD' not found

但如果使用

而不是@importfrom语句
#' @import data.table

运行正常。我不想使用后者,因为一些函数名称与其他包冲突。如何导入.SD - 也许这不是可导入的函数?

1 个答案:

答案 0 :(得分:2)

.SD根本不是一个功能。

In the source code of the package您可以看到,.SD仅导出以防止注意:

.SD = .N = .I = .GRP = .BY = .EACHI = NULL
# These are exported to prevent NOTEs from R CMD check, and checkUsage via compiler.
# But also exporting them makes it clear (to users and other packages) that data.table uses these as symbols.
# And NULL makes it clear (to the R's mask check on loading) that they're variables not functions.
# utils::globalVariables(c(".SD",".N")) was tried as well, but exporting seems better.
# So even though .BY doesn't appear in this file, it should still be NULL here and exported because it's
# defined in SDenv and can be used by users.

您可以尝试从'special-symbols'导入data.table

#' @importFrom data.table "special-symbols"

或者,您可以将此行.SD = .N = .I = .GRP = .BY = .EACHI = NULL添加到您的包中。