Escaping backslash (\) in string or paths in R

From R 4.0.0 you can use r"(...)" to write a path as raw string constant, which avoids the need for escaping:

r"(E:\RStuff\test.r)"
# [1] "E:\\RStuff\\test.r"

There is a new syntax for specifying raw character constants similar to the one used in C++: r"(...)" with ... any character sequence not containing the sequence )". This makes it easier to write strings that contain backslashes or both single and double quotes. For more details see ?Quotes.

Leave a Comment