data.table 中的 J() 函数是如何实现的? [英] How is J() function implemented in data.table?

查看:8
本文介绍了data.table 中的 J() 函数是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我下载了源代码,但在任何地方都找不到这个 J 函数的定义.我找到了 lockBind(".SD", ...),但没有找到 J.知道这个功能是如何实现的吗?

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() 以前是导出的,1.8.8 以后就没有了.这是 1.8.8 的注释:

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

o J() 别名现在已在 outside DT[...] 中移除,但仍可在 DT 内部使用[...];即, DT[J(...)] 很好.正如 v1.8.2 中的警告(请参阅此文件中的下文)并在 v1.8.4 中被 warning() 弃用.这解决了与包 XLConnect (#1747) 和 rJava (#2045) 中的函数 J() 的冲突.请在 DT[...] 之外直接使用 data.table() 而不是 J().

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(.) 并使用(不可见的)非导出函数 .massagei.

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

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

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)) 的类型并执行此行:

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

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

其中 isub = substitution(i).massagei 很简单:

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

.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 必须发生.

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 中的 J() 函数是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆