简体   繁体   English

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

[英]how to @importFrom data.table within r package function

I have a function in a package I am developing. 我正在开发的程序包中有一个功能。 I don't think the input data is relevant so I haven't posted it. 我认为输入数据无关紧要,所以我还没有发布。 I am trying to only import specific functions from packages into this function as per recommendations and this mostly works fine apart from for data.table. 我试图按照建议仅将包中的特定功能导入此功能,并且除了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)
    }

If I do as above I get the error: 如果执行上述操作,则会收到错误消息:

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

but if instead of the @importfrom statement I use 但是如果我使用@importfrom语句代替

#' @import data.table

it runs fine. 运行正常。 I don't want to use the latter as some of the function names clash with other packages. 我不想使用后者,因为某些函数名称与其他软件包冲突。 How can I import .SD - perhaps this isn't the importable function? 如何导入.SD也许这不是可导入函数?

.SD is not a function at all. .SD根本不是功能。

In the source code of the package you can see, that .SD is only exported to prevent NOTEs: 在软件包的源代码中,您可以看到,仅导出.SD是为了防止出现.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.

You can try to import 'special-symbols' from data.table : 您可以尝试从data.table导入'special-symbols'

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

ALternatively, you could just add this line .SD = .N = .I = .GRP = .BY = .EACHI = NULL to your package. 另外,您也可以在.SD = .N = .I = .GRP = .BY = .EACHI = NULL添加.SD = .N = .I = .GRP = .BY = .EACHI = NULL这行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM