I would like to use gsub to replace every occurrence of a backslash in a string with 2 backslashes.
Currently, what I have I tried is gsub("\\\\", "\\", x). This doesn't seem to work though. However, if I change the expression to instead replace each backslash with "a", it works fine.
> gsub("\\\\", "\\", "\\")
[1] ""
> gsub("\\\\", "a", "\\")
[1] "a"
> gsub("\\\\", "\\\\", "\\")
[1] "\\"
The last character is only a single backslash; R just prints 2 because it prints escaped characters with the backslash. Using nchar confirms that the length is 1.
What causes this functionality? The second argument to gsub isn't a regular expression, so having 4 backslashes in the string literal should be converted to a character with 2 backslashes. It makes even less sense that the first gsub call above returns an empty string.