How to do string interpilation in R

44 views Asked by At

Basically the function where you add a variable inside a string like the JavaScript template literals:

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`)

overall I just need a simple function that would make my life easier with R instead of going like:

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

I tried checking for easy stuff but it takes too long, like

paste(string1, var, string2, collapse = NULL) and stuff but its just too annoying and would be simpler with str ${var} str

1

There are 1 answers

0
Arthur On BEST ANSWER

I think str_glue is what you're looking for.

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

Created on 2024-02-28 with reprex v2.0.2