r"{nav\to\file\path.ext}"
[1] "nav\\to\\file\\path.ext"
The one thing about Python I actually really like is the ability to use raw strings. Raw strings are super helpful for me because at work I use a windows machine. And windows machines use a silly file path convention. The \
back slack character is used as the file separator as opposed to the linux / unix /
forward slash.
Using the backslash is so annoying because it’s also an escape character. In python I can write the following to hard code a file path.
= r"nav\to\file\path.ext" file_path
Whereas in R typically you would have to write:
file_path <- "nav\\to\\file\\path.ext"
Since \
is an escape character you have to escape it first using itself. So, its annoying. And file.path("nav", "to", "file", "path.ext", fsep = "\\")
is a wee bit cumbersome sometimes.
So like, you can use raw strings today.
It was announced in the news for version 4.0.0.
They write:
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.
You can write raw strings using the following formats:
r"( ... )"
r"{ ... }"
r"[ ... ]"
R"( ... )"
R"{ ... }"
R"[ ... ]"
You can even trickier by adding dashes between the quote and the delimter. The dashes need to be symmetrical though. So the following is also valid.
r"-{ ... }"-
r"--{ ... }--"
r"--{ * _ * }--"
Alright so back to the example
r"{nav\to\file\path.ext}"
[1] "nav\\to\\file\\path.ext"
Hot damn. Thats nice.
I freaked out at first though because R prints two backslashes. But if you cat the result they go away. So do not worry.
r"{nav\to\file\path.ext}" |>
cat()
nav\to\file\path.ext