Skip to content
Prev 200339 / 398503 Next

How to get the string '\'?

On Nov 15, 2009, at 6:35 PM, Peng Yu wrote:

            
No,  that was most definitely _not_ your posed question. If you want  
now to change your question and supply a reproducible example, that's  
fine, just don't claim that your mind should have been read more  
properly that it was, .... please.

The problem with your _second_ question is that the printed  
representation of "\" is a problem because of its special use as an  
escape symbol. So sometimes it needs to be displayed as "\\". What  
gets written to the screen may be different that the internal  
representation. Look at the results of:
 > string='abc/efg'
 > cat(gsub('/','\\\\',string), file="test.txt")

You should see:
abc\efg

...although at the screen you would see:

 > string='abc/efg'
 > gsub('/','\\\\',string)
[1] "abc\\efg"

The first "\" escapes second "\" which in turn allows whatever follows  
to be interpreted as "escaped", while the third "\" escapes the 4th  
"\" so that it can be examined by the R interpreter as a real "\".