【发布时间】:2014-05-26 17:25:39
【问题描述】:
我正在尝试编写一个函数,该函数使 R 解释参数而不评估它们并将它们推入一个新字符串。
这是我目前所拥有的。它非常难看,但最多可以使用 5 个:
pl <- pasteliteral <-
function(v1='',v2='',v3='',v4='',v5='', sep="") {
stopifnot(v1!="")
# Sort up to 10 values
if (deparse(substitute(v1)!="")) {s<-deparse(substitute(v1))
if (deparse(substitute(v2)!="")) {s<-paste(s, deparse(substitute(v2)), sep=sep)
if (deparse(substitute(v3)!="")) {s<-paste(s, deparse(substitute(v3)), sep=sep)
if (deparse(substitute(v4)!="")) {s<-paste(s, deparse(substitute(v4)), sep=sep)
if (deparse(substitute(v5)!="")) {s<-paste(s, deparse(substitute(v5)), sep=sep)
}}}}}
s
}
pl(1,2,3,a,hello)
[1] "123ahello"
【问题讨论】:
-
而且我知道这个函数没有可能的用途,除非一旦我拥有它,我会修改它以检查每个作为字符的参数是否存在,如果存在,我会将它们粘贴在一起.
-
你可以用
as.list(substitute(list(1,2,3,a,hello)))[-1]然后lapplydeparse和do.callpaste收集你的论点。