如何在 R 中进行字符串插值[重复]

问题描述 投票:0回答:1

基本上就是在字符串中添加变量的函数,就像 JavaScript 模板文字一样:

var str = "This is a sample";

print(`So you load the string in with ${str}, and it would insert the string without going "string" + var + "string" or using the paste() function in r, so I need help`)

总的来说,我只需要一个简单的函数,它可以让我的 R 生活更轻松,而不是像这样:

str <- "STRING"
print(paste("this is very annoying to do to require a", str, "so i need simpler options", sep="", collape=NULL))

我尝试检查简单的东西,但花了太长时间,比如

paste(string1, var, string2, collapse = NULL)
之类的东西,但它太烦人了 用
str ${var} str

会更简单
r formatting string-formatting
1个回答
1
投票

我认为

str_glue
就是您正在寻找的。

library(stringr)
str <- "STRING"
str_glue("when you require a {str}, this is a simpler option")
#> when you require a STRING, this is a simpler option

创建于 2024-02-28,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.