代码之家  ›  专栏  ›  技术社区  ›  moodymudskipper

中点函数`.()`的实现数据表包装[副本]

  •  3
  • moodymudskipper  · 技术社区  · 6 年前

    ?data.table::data.table :

    表达式“.”()“是list()的简写别名;它们都表示 相同的

    然而,这个函数却无处可寻:

    data.table:::.
    


    找不到对象“.”

    以下方法效果不错:

    test  <- function(x) {
      eval(substitute(
        eval.parent(substitute(x, list(.=list)))
        ))
    }
    
    foo <- "bar"
    test(.(foo))
    # [[1]]
    # [1] "bar"
    identical(test(.(foo)), list(foo))
    # [1] TRUE
    

    . <- "baz"
    test(.(foo,.))
    # [[1]]
    # [1] "bar"
    # 
    # [[2]]
    # function (...)  .Primitive("list")
    

    预期:

    # [[1]]
    # [1] "bar"
    # 
    # [[2]]
    # [1] "baz"
    
    1 回复  |  直到 6 年前
        1
  •  7
  •   duckmayr    6 年前

    这个 data.table 包用这段代码来完成它

    replace_dot_alias <- function(e) {
      # we don't just simply alias .=list because i) list is a primitive (faster to iterate) and ii) we test for use
      # of "list" in several places so it saves having to remember to write "." || "list" in those places
      if (is.call(e)) {
        # . alias also used within bquote, #1912
        if (e[[1L]] == 'bquote') return(e)
        if (e[[1L]] == ".") e[[1L]] = quote(list)
        for (i in seq_along(e)[-1L]) if (!is.null(e[[i]])) e[[i]] = replace_dot_alias(e[[i]])
      }
      e
    }
    

    发现于 R/data.table.R (目前在第173行)。所以你找不到 data.table:::.

    然后在 [.data.table" <- function (x, i, j, ... 他们可以做这种事

    if (!missing(j)) {
        jsub = replace_dot_alias(substitute(j))
        root = if (is.call(jsub)) as.character(jsub[[1L]])[1L] else ""