R:你能不能让我的丑文concacting功能pretty [英] R: Can you make my ugly text concacting function pretty
本文介绍了R:你能不能让我的丑文concacting功能pretty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图写一个函数是什么让 - [R间preT参数没有评估并将其推入一个新的字符串。
这是我迄今。这是非常难看,但工作长达5:
PL< - pasteliteral< -
函数(V1 ='',V2 ='',V3 ='',V4 ='',V5 ='',九月=){ stopifnot(V1!=) #排序多达10个值
如果(deparse(替代(V1)=)!){S< -deparse(替代(V1))
如果(deparse(替代(V2)=)!){S< - 粘贴(S,deparse(替代(V2)),=九月SEP)
如果(deparse(替代(V3)=)!){S< - 粘贴(S,deparse(替代(V3)),=九月SEP)
如果(deparse(替代(V4)=)!){S< - 粘贴(S,deparse(替代(V4)),=九月SEP)
如果(deparse(替代(V5)=)!){S< - 粘贴(S,deparse(替代(V5)),=九月SEP)
}}}}}
小号
}PL(1,2,3,A,你好)
[1]123ahello
解决方案
您可以尝试 sys.call
:
PL< - pasteliteral< - 功能(... 09月=){
##取出一个元素(函数名)
的Ca; - sys.call() - 1] ##取出最后一个元素,如果它是九月
如果(CA [长度(CA)]] == SEP){
的Ca; - CA [-length(CA)
}
paste0(CA,崩溃= SEP)
}PL(1,2,3,A,你好)
#[1]123ahelloPL(1,2,3,A,你好,月=)
#[1]1:2:3:A:你好
I am trying to write a function what makes R interpret arguments without evaluating them and pushes them into a new string.
This is what I have so far. It is very ugly but works for up to 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"
解决方案
You can try sys.call
:
pl <- pasteliteral <- function(..., sep="") {
## remove first element (the function name)
ca <- sys.call()[-1]
## remove last element if it is sep
if (ca[[length(ca)]] == sep) {
ca <- ca[-length(ca)]
}
paste0(ca, collapse=sep)
}
pl(1,2,3,a,hello)
# [1] "123ahello"
pl(1,2,3,a,hello, sep=":")
# [1] "1:2:3:a:hello"
这篇关于R:你能不能让我的丑文concacting功能pretty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文