如何在data.table中实现J()函数?

时间:2022-06-13 14:30:10

I recently learned about the elegant R package data.table. I am very curious to know how the J function is implemented there. This function is bound to the function [.data.table, it doesn't exist in the global environment.

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

I downloaded the source code but I cannot find the definition for this J function anywhere there. I found lockBind(".SD", ...), but not J. Any idea how this function is implemented?

我下载了源代码但是我无法在那里找到这个J函数的定义。我找到了lockBind(“。SD”,......),但没有找到J.还知道这个函数是如何实现的吗?

Many thanks.

非常感谢。

1 个解决方案

#1


14  

J() used to be exported before, but not since 1.8.8. Here's the note from 1.8.8:

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[...].

o现在在DT [...]之外删除了J()别名,但仍然可以在DT [...]内部工作;即,DT [J(...)]没问题。正如在v1.8.2中所述(在此文件中见下文)并在v1.8.4中弃用了warning()。这解决了与包XLConnect(#1747)和rJava(#2045)中的函数J()的冲突。请在DT [...]之外直接使用data.table()而不是J()。

Using R's lazy evaluation, J(.) is detected and simply replaced with list(.) using the (invisible) non-exported function .massagei.

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

That is, when you do:

也就是说,当你这样做时:

require(data.table)
DT = data.table(x=rep(1:5, each=2L), y=1:10, key="x")
DT[J(1L)]

i (= J(1L)) is checked for its type and this line gets executed:

检查i(= J(1L))的类型,并执行此行:

i = eval(.massagei(isub), x, parent.frame())

where isub = substitute(i) and .massagei is simply:

其中isub = substitute(i)和.massagei只是:

.massagei = function(x) {
    if (is.call(x) && as.character(x[[1L]]) %chin% c("J","."))
        x[[1L]] = quote(list)
    x
}

Basically, data.table:::.massagei(quote(J(1L))) gets executed which returns list(1L), which is then converted to data.table. And from there, it's clear that a join has to happen.

基本上,data.table :::。massagei(quote(J(1L)))被执行,返回list(1L),然后转换为data.table。从那里开始,很明显必须进行联接。

#1


14  

J() used to be exported before, but not since 1.8.8. Here's the note from 1.8.8:

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[...].

o现在在DT [...]之外删除了J()别名,但仍然可以在DT [...]内部工作;即,DT [J(...)]没问题。正如在v1.8.2中所述(在此文件中见下文)并在v1.8.4中弃用了warning()。这解决了与包XLConnect(#1747)和rJava(#2045)中的函数J()的冲突。请在DT [...]之外直接使用data.table()而不是J()。

Using R's lazy evaluation, J(.) is detected and simply replaced with list(.) using the (invisible) non-exported function .massagei.

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

That is, when you do:

也就是说,当你这样做时:

require(data.table)
DT = data.table(x=rep(1:5, each=2L), y=1:10, key="x")
DT[J(1L)]

i (= J(1L)) is checked for its type and this line gets executed:

检查i(= J(1L))的类型,并执行此行:

i = eval(.massagei(isub), x, parent.frame())

where isub = substitute(i) and .massagei is simply:

其中isub = substitute(i)和.massagei只是:

.massagei = function(x) {
    if (is.call(x) && as.character(x[[1L]]) %chin% c("J","."))
        x[[1L]] = quote(list)
    x
}

Basically, data.table:::.massagei(quote(J(1L))) gets executed which returns list(1L), which is then converted to data.table. And from there, it's clear that a join has to happen.

基本上,data.table :::。massagei(quote(J(1L)))被执行,返回list(1L),然后转换为data.table。从那里开始,很明显必须进行联接。