Dear R List I have a small problem concerning the output of print(). My version:> R.version_ platform i386-portbld-freebsd5.2 arch i386 os freebsd5.2 system i386, freebsd5.2 status major 1 minor 9.0 year 2004 month 04 day 12 language R 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? Regards, Kevin
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
On Tue, 30 Nov 2004, Kevin Brinkmann wrote:> Dear R List > > I have a small problem concerning the output of print(). > > My version: > >> R.version > _ > platform i386-portbld-freebsd5.2 > arch i386 > os freebsd5.2 > system i386, freebsd5.2 > status > major 1 > minor 9.0 > year 2004 > month 04 > day 12 > language R > > 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?Because it is documented to escape backslashes: use cat() if that is not what you want. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Kevin Brinkmann > Sent: Tuesday, November 30, 2004 9:43 AM > To: R-help at stat.math.ethz.ch > Subject: [R] Problem with print() and backslashes. > > > Dear R List > > I have a small problem concerning the output of print(). ><snip></snip>> > 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?> cat("\\!")\! also> str("\\!")chr "\!" What is important to keep in mind is that the string "\\!" has *two* characters (when read by R), not three (as on the screen or in your editor);> nchar("\\!")[1] 2 The first character is "\\", which needs to be escaped in order for the R parser to recognize it as '\'. The second is of course '!'. I agree that> nchar("\!")[1] 1 might be confusing. The thing is that some characters remain the same escaped or not, whereas others have certain mappings to non-printable ASCII codes (0-255).> identical("\!", "!")[1] TRUE and the well known(?) newline character> identical("\n", "n")[1] FALSE Another good example:> identical("\"", '"')[1] TRUE Given a string, print() gives you the escaped version of the string, which can be useful for debugging, if you want to cut'n'paste and so on. Thus,> print("Hello world\n!\n")[1] "Hello world\n!\n" and> cat("Hello world\n!\n")Hello world ! Hope this helps! Henrik Bengtsson> Regards, > > Kevin > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Seemingly Similar Threads
- Data editor duplicates pairs of backslashes (PR#11897)
- Gnome-terminal's backslashes look like Ws with a horizontal line through -- how to get a backslash?
- gsub: replacing double backslashes with single backslash
- Removing backslashes from data
- gsub + backslashes