Using data.table's .() shortcut in quoted expr

2019-04-23 14:52发布

问题:

I have some data.tables containing file names as a var named fn. I want to split off basename and extension:

library(data.table)
library(tools)

DT1 = data.table(fn = c("gah.csv", "egad.csv"))
DT2 = data.table(fn = c("gah.xlsx", "egad.xlsx"))
DT3 = data.table(fn = c("boo.txt", "ya.foo"))

do_split_fn = quote(c("name", "ext") := list(file_path_sans_ext(fn), file_ext(fn)))

DT1[, eval(do_split_fn)]
DT2[, eval(do_split_fn)]
DT3[, eval(do_split_fn)]

So this all works fine and my question is very minor: Can I use an expression more like this?

do_split_fn_dot = quote(c("name", "ext") := .(file_path_sans_ext(fn), file_ext(fn)))
DT1[, eval(do_split_fn_dot)]
# Error in eval(expr, envir, enclos) : could not find function "."

That is, I'm trying to swap list() for .(), as one can do inside `[.data.table`.

My quote/eval stuff is an attempt at following recommendations in the data.table FAQ 1.6.

回答1:

Already fixed

library(data.table)
library(tools)

DT1 = data.table(fn = c("gah.csv", "egad.csv"))

do_split_fn_dot = quote(c("name", "ext") := .(file_path_sans_ext(fn), file_ext(fn)))
DT1[, eval(do_split_fn_dot)]
DT1
#         fn   name    ext
#1:  gah.csv    gah    csv
#2: egad.csv   egad    csv