Search code examples
rstringrr-glue

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


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"

Solution

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