`stringr::str_glue`: how to escape backslash (\)?

535 Views Asked by At

I'd like str_glue to keep \ when I glue, but I don't understand how to escape it.

I have this reprex with the logical attempts I've made: (escaping the escape, etc)

library(stringr)
var <- "darkness"

str_glue("Hello {var} my old friend \
I've come to talk with you again")
#> Hello darkness my old friend 
#> I've come to talk with you again

str_glue("Hello {var} my old friend \\
I've come to talk with you again")
#> Hello darkness my old friend I've come to talk with you again

str_glue("Hello {var} my old friend \\\
I've come to talk with you again")
#> Hello darkness my old friend I've come to talk with you again

Created on 2021-04-28 by the reprex package (v2.0.0)

My desired output:

"Hello darkness my old friend \
I've come to talk with you again"
1

There are 1 best solutions below

0
On BEST ANSWER
var <- "darkness"
stringr::str_glue("Hello {var} my old friend \\\ \n I've come to talk with you again")
#> Hello darkness my old friend \ 
#> I've come to talk with you again

Created on 2021-04-28 by the reprex package (v2.0.0)