在改进rbind方法时,我想提取传递给它的对象的名称,以便我可以从中生成唯一的ID.
我试过了,all.names(match.call())但这只是给了我:
[1] "rbind" "deparse.level" "..1" "..2"
Run Code Online (Sandbox Code Playgroud)
通用示例:
rbind.test <- function(...) {
dots <- list(...)
all.names(match.call())
}
t1 <- t2 <- ""
class(t1) <- class(t2) <- "test"
> rbind(t1,t2)
[1] "rbind" "deparse.level" "..1" "..2"
Run Code Online (Sandbox Code Playgroud)
而我希望能够检索c("t1","t2").
我知道一般来说,一个人无法检索传递给函数的对象的名称,但似乎有......可能,如上例中的substitute(...)返回t1.
Tyl*_*ker 13
我在R帮助列表服务中从Bill Dunlap那里选了这个:
rbind.test <- function(...) {
sapply(substitute(...()), as.character)
}
Run Code Online (Sandbox Code Playgroud)
我认为这会给你你想要的东西.
使用此处的指导如何在编写自己的函数时使用R的省略号功能?
例如 substitute(list(...))
并与...结合 as.character
rbind.test <- function(...) {
.x <- as.list(substitute(list(...)))[-1]
as.character(.x)
}
Run Code Online (Sandbox Code Playgroud)
你也可以用
rbind.test <- function(...){as.character(match.call(expand.dots = F)$...)}
Run Code Online (Sandbox Code Playgroud)