Are there raw strings in R for regular expressions?
Question: 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()`?


Answer: Not in exactly the way you want (i.e. not a language-wide specification for
raw strings), and not clearly documented, but you can achieve what I interpret
as your **broader goal** (doing string replacement without fighting with
unwieldy specifications of target patterns) in `stringr` functions by setting
the class of the search pattern.

    
    
    s <- "\\"
    class(s) <- "fixed"
    str_replace("back\\slash", s, ":")
    

Unfortunately `setClass()` means something other than "set the class attribute
of an object on the fly", so you might want to define

    
    
    str_replace_fixed <- function(string, pattern, replacement) {
        class(pattern) <- "fixed"
        return(str_replace(string,pattern,replacement))
    }
    

There is a `str_split_fixed()` in `stringr` already; you could request a
`str_replace_fixed` be added to the package ... (if I were posting an [issue
on the stringr Github repo](https://github.com/hadley/stringr/issues) I might
ask first that this feature be clearly documented, and secondarily that
`str_split_fixed` be implemented ...)

And as pointed out in the comments, most of the base-R string manipulation
functions (`sub`, `gsub`, `grep`, `grepl`) already have a `fixed` argument ...

