【发布时间】:2020-07-10 06:50:49
【问题描述】:
我有一个data.table,一个带有名字的字符串和一个函数:
example_dt <- data.table(a = c(1,2,3), b = c(4,5,6))
string <- 'example_dt'
fun <- function(x) {
print((deparse(substitute(x))))
x[c(1,2), c(1,2)]
}
使用 data.table 作为参数调用函数时,一切正常。
> fun(example_dt)
[1] "example_dt"
a b
1: 1 4
2: 2 5
当然,用字符串调用是行不通的。
> fun(string)
[1] "string"
Error in x[c(1, 2), c(1, 2)] : número incorreto de dimensões
我可以使用 get 解决这个问题,但是我丢失了有关 data.table 名称的信息。
> fun(get(string))
[1] "get(string)"
a b
1: 1 4
2: 2 5
知道如何使用字符串调用函数,同时检索 data.table 的原始名称“example_dt”吗?
【问题讨论】: