Sorry, no idea how with plain print()
(though that's not to say it's impossible—someone else might know how).
The other alternative is to use writeLines()
, which uses the same mechanism as cat()
(i.e. doesn't print escape characters). You can see, for example, this is used in the Escaping section of the Regular Expressions vignette for stringr, since it's how you show the output unescaped.
library(stringr)
string <- "This is a string with double backslashes \\"
string
#> [1] "This is a string with double backslashes \\"
writeLines(str_replace(string, "\\\\", "\\\\"))
#> This is a string with double backslashes \
Created on 2023-04-03 with reprex v2.0.2
Edit Not sure I get your example with "Hello my name is" since that doesn't have backslashes. That would work fine with print()
.