这可以很容易地完成,根本不需要任何包。以下是一些可能性:
# 1
paste("All my variables:", toString(vars))
## [1] "All my variables: a, b, c"
# 2
sprintf("All my variables: %s", toString(vars))
## [1] "All my variables: a, b, c"
# 3
sub("@", toString(vars), "All my variables: @")
## [1] "All my variables: a, b, c"
如果您希望这样做以输出警告或错误消息:
# 4a
warning("All my variables: ", toString(vars))
## Warning message:
## All my variables: a, b, c
# 4b
stop("All my variables: ", toString(vars))
## Error: All my variables: a, b, c
来自gsubfnfn$
包。在任何函数调用前加上fn$
(例如c
此处),然后将使用准 perl 字符串插值处理参数。
# 5
library(gsubfn)
fn$c("All my variables: `toString(vars)`")
## [1] "All my variables: a, b, c"
或者
# 6
library(gsubfn)
string <- toString(vars)
fn$c("All my variables: $string")
## [1] "All my variables: a, b, c"