r
Author

Josiah Parry

Published

January 19, 2023

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.

file_path = r"nav\to\file\path.ext"

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.

IT’s VERSION 4.2.2

Aight.

So like, you can use raw strings today.

How can I get the R-devel news? I’m on the mailing list and get it once a week and it’s like “Re: memory leak in png()  not this stuff. Tips?

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"--{ * _ * }--"

It kinda looks like a crab

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`