[R] Problem with print() and backslashes.

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Nov 30 09:59:52 CET 2004


Kevin Brinkmann <kbrinkm at ump.gwdg.de> writes:

> Consider this: I want to print a backslash with an exclamation mark. Here
> is the output.
> 
> > print( "\!" )
> [1] "!"
> 
> Now I try it differently...
> 
> > print( "\\!" )
> [1] "\\!"
> 
> The output contains two backslashes. Why?

(Didn't we do that one only yesterday?)

The answer is: For the same reason that you need them on input. R
likes to line things up in columns when printing vectors so cannot
just print special characters like newline, carriage return, etc.
Instead it represents them as \n, \r just like on input. To
distinguish from backslash-followed-by-n the backslash is itself
escaped with a backslash. Use cat() to output a string as raw
characters. 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list