在R中,与"VAs"相同的是什么?

Ben*_*aua 3 excel vba r excel-vba

在excel(和Excel VBA)中,使用"&"连接文本和变量非常有用:

a = 5
msgbox "The value is: " & a
Run Code Online (Sandbox Code Playgroud)

会给

"The value is: 5"
Run Code Online (Sandbox Code Playgroud)

我怎么能在R中这样做?我知道有一种方法可以使用" 粘贴 ".但是我想知道是否有任何技巧可以像在Excel VBA中那样简单.

提前致谢.

jak*_*kub 5

这篇博文建议定义自己的连接运算符,类似于VBA(和Javascript)所具有的功能,但它保留了以下功能paste:

"%+%" <- function(...) paste0(..., sep = "")

"Concatenate hits " %+% "and this."
# [1] "Concatenate hits and this."
Run Code Online (Sandbox Code Playgroud)

我不是这个解决方案的忠实粉丝,因为它有点模糊paste了引擎盖下的内容.例如,你会直觉这会发生吗?

"Concatenate this string " %+% "with this vector: " %+% 1:3
# [1] "Concatenate this string with this vector: 1"
# [2] "Concatenate this string with this vector: 2"
# [3] "Concatenate this string with this vector: 3"
Run Code Online (Sandbox Code Playgroud)

例如,在Javascript中,这会给你Concatenate this string with this vector: 1,2,3,这是完全不同的.我不能代表Excel,但你应该考虑一下这个解决方案对你来说是否有点困惑而不是有用.

如果你需要类似Javascript的解决方案,你也可以试试这个:

"%+%" <- function(...) {
   dots = list(...)
   dots = rapply(dots, paste, collapse = ",")
   paste(dots, collapse = "")
}

"Concatenate this string " %+% "with this string."
# [1] "Concatenate this string with this string."

"Concatenate this string " %+% "with this vector: " %+% 1:3
# [1] "Concatenate this string with this vector: 1,2,3"
Run Code Online (Sandbox Code Playgroud)

但是我还没有进行过广泛的测试,因此请留意意外的结果.