r - data.table 中的 J() 函数是如何实现的?

标签 r data.table

我最近了解了优雅的 R 包 data.table .我很想知道J功能在那里实现。此函数绑定(bind)到函数 [.data.table ,它在全局环境中不存在。

我下载了源代码,但找不到此 J 的定义在那里的任何地方发挥作用。我找到了lockBind(".SD", ...) ,但不是 J .知道这个功能是如何实现的吗?

非常感谢。

最佳答案

J()以前是导出的,但从 1.8.8 开始就没有了。这是来自 1.8.8 的注释:

o The J() alias is now removed outside DT[...], but will still work inside DT[...]; i.e., DT[J(...)] is fine. As warned in v1.8.2 (see below in this file) and deprecated with warning() in v1.8.4. This resolves the conflict with function J() in package XLConnect (#1747) and rJava (#2045). Please use data.table() directly instead of J(), outside DT[...].



使用 R 的惰性求值,J(.)被检测到并简单地替换为 list(.)使用(不可见的)非导出函数 .massagei .

也就是说,当你这样做时:
require(data.table)
DT = data.table(x=rep(1:5, each=2L), y=1:10, key="x")
DT[J(1L)]
i (= J(1L) ) 检查其类型并执行此行:
i = eval(.massagei(isub), x, parent.frame())

在哪里 isub = substitute(i).massagei很简单:
.massagei = function(x) {
    if (is.call(x) && as.character(x[[1L]]) %chin% c("J","."))
        x[[1L]] = quote(list)
    x
}

基本上,data.table:::.massagei(quote(J(1L)))执行返回 list(1L) , 然后转换为 data.table .从那里,很明显 join必须发生。

关于r - data.table 中的 J() 函数是如何实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22001945/

相关文章:

regex - 使用 Regex 修改 CSV 中的特定列

r - 使用 grepl 搜索文本中的多个子字符串之一

r - cbind vs rbind与data.table

r - 使用 count()、aggregate()、data.table() 或 dplyr() 汇总数据(均值、标准差)

rCharts nvd3 库强制刻度

r - "Reversed"在 ggplot2 中使用 fct_infreq()

r - 绘制二分加折线图比较

r - 将值分配给特定的data.table列和行

performance - R中数据的快速边界

r - R中的多线程数据表比使用单线程的慢得多