R:你可以让我丑陋的文字简洁功能

Fra*_*art 0 arguments r function

我试图编写一个函数,使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"
Run Code Online (Sandbox Code Playgroud)

sgi*_*ibb 6

你可以尝试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"
Run Code Online (Sandbox Code Playgroud)