I want to output my R analysis using knitr, and I have text containing a line of underscores. I have to make them escape to turn everyone _in \_.
But since the backslash is also a special character in the regular expression, I have not found a way to actually have one backslash inserted before each underscore. It seems that the odd numbers of backslashes are causing an error (I think the latter was paired with an underscore), but trying to escape with even numbers didn't work either.
a <- "blah _ blah ___ blah"
> gsub("_", "\\_", a)
[1] "blah _ blah ___ blah"
> gsub("_", "\\\\_", a)
[1] "blah \\_ blah \\_\\_\\_ blah"
> gsub("_", "\\\_", a)
Error: '\_' is an unrecognized escape in character string starting ""\\\_"
> gsub("_", "\_", a)
Error: '\_' is an unrecognized escape in character string starting ""\_"
What is the right way? It should not use gsub and regex, but I need the escape sequences to be easily applied to the same line.