27

In Python you can use raw strings:

import re
re.sub(r"\\", ":", "back\\slash")  # r"\\" instead of "\\\\"

Does this exist in R as well? For example, here is an equivalent code snippet without raw strings in R:

library(stringr)
str_replace("back\\slash", "\\\\", ":")

I would love to be able to do this:

str_replace("back\\slash", raw("\\"), ":")

Does this functionality already exist, or should I just implement my own function raw()?

Megatron
  • 15,909
  • 12
  • 89
  • 97

2 Answers2

48

As of R 4.0.0 this is available.

Raw character constants are also available using a syntax similar to the one used in C++: r"(...)" with ... any character sequence, except that it must not contain the closing sequence )". The delimiter pairs [] and {} [c]an also be used. For additional flexibility, a number of dashes can be placed between the opening quote and the opening delimiter, as long as the same number of dashes appear between the closing delimiter and the closing quote.

From the example in ?Quotes:

r"{(\1\2)}"
## [1] "(\\1\\2)"

(note, the double-backslashes are R's printed representation of backslashes: cat() on this object will print (\1\2))

From @Mael in comments: to solve the OP's original question,

stringr::str_replace("back\\slash", r"(\\)", ":")
## or
gsub(r"(\\)", ":", "back\\slash")

(the latter is very close to Python's syntax)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • 2
    This is good news that this is being discussed seriously or is under development and will be addressed in the foreseeable future. Thanks for responding. Perhaps this is too much to ask, but another "great simplifier" feature in Python that I find useful for its consistency and utility is the docstring object syntax, i.e., using balanced triple quote marks (""" docstring doublequote syntax """ or ''' docstring single quote syntax ''') for multi-line comments and internal documentation strings. I'll have to request it from the R core language developers. – Rich Lysakowski PhD Feb 21 '20 at 17:24
  • The `r-devel@r-project.org` mailing list is the right place to raise such requests. (Note, R at present doesn't have such a convention for "internal documentation strings" - although I would argue that the roxygen(2) system is fuller-featured than Python docstrings ...) – Ben Bolker Feb 21 '20 at 22:43
  • Sadly, this does not fix when passing regex in SQL to, in my case, Athena. – Climbs_lika_Spyder Feb 23 '21 at 20:33
  • As per OP's example, one should do: `str_replace("back\\slash", r"(\\)", ":")` – Maël Jan 25 '22 at 18:41
  • 1
    `r"--[ ... ]--"` This is valid too. – GitHunter0 Apr 28 '22 at 02:32
2

For your example a R < 4.0.0 alternative would be the other way round to use a function that automatically escapes special characters. Here the stringi package is helpful.

library(stringr)
library(stringi)
str_replace("back\\slash", stri_escape_unicode("\\"), ":")

Since this is very verbose defining r <- stri_escape_unicode would come close to your desired functionality (r("\\")).

The stringi package also has a function to reverse escaping stri_unescape_unicode which is useful in a shiny apps, where user inputs are automatically escaped.

TimTeaFan
  • 17,549
  • 4
  • 18
  • 39