如何将变量名称作为列表中的字符

时间:2015-08-26 18:47:08

标签: r

我有一个未命名的变量列表,我想将此列表输入到函数中并获取变量的名称。

l <- list(cars,mtcars)
fun <- function(l){...}
f(l)
 # c("cars", "mtcars")

我一直在玩deparse(substitute(...))但没有成功。 有任何想法吗?

这里有一些我尝试过的事情:

l <- list(cars,mtcars)

lapply(l,function(i){deparse(substitute(i))})
lapply(l,function(i){deparse(substitute(i,parent.frame()))})
lapply(l,function(i){deparse(substitute(i,parent.frame(sys.nframe())))})

Map(function(i){deparse(substitute(i))},l)
Map(function(i){deparse(substitute(i,parent.frame(sys.nframe())))},l)

foo <- function(a, ...) {
  arg <- deparse(substitute(a))
  dots <- substitute(list(...))[-1]
  c(arg, sapply(dots, deparse))
}
foo(cars)
do.call("foo",l)

foo2 <- function(x) deparse(substitute(x))
foo2(mtcars)
do.call("foo",l)

foo3 <- function(a, ...) {
  sapply(match.call(expand.dots=TRUE)[-1], deparse)
}
foo3(cars)
do.call("foo3",l)

0 个答案:

没有答案
相关问题