baoliqin at gmail.com
2009-Dec-21 12:00 UTC
[Rd] a little bug for the function 'sprintf' (PR#14161)
Dear R-ers, I am a gratuate student from South China University of Technology. I fond the function 'sprintf' in R2.10.1 have a little bug(?): When you type in the example codes:> sprintf("%s is %f feet tall\n", "Sven", 7.1)and R returns: [1] "Sven is 7.100000 feet tall\n" this is very different from the 'sprintf' function in C/C++, for in C/C++, the format string "\n" usually represents a new line, but here, just the plain text "\n"! Is it a bug, or a deliberate design? Thanks for your attention! Best wishes! Yours sincerely, Baoli Qin [[alternative HTML version deleted]]
Peter Dalgaard
2009-Dec-21 17:36 UTC
[Rd] a little bug for the function 'sprintf' (PR#14161)
baoliqin at gmail.com wrote:> Dear R-ers, > > I am a gratuate student from South China University of Technology. I fond > the function 'sprintf' in R2.10.1 have a little bug(?): > > When you type in the example codes: > >> sprintf("%s is %f feet tall\n", "Sven", 7.1) > > and R returns: > > [1] "Sven is 7.100000 feet tall\n" > > this is very different from the 'sprintf' function in C/C++, for in C/C++, > the format string "\n" usually represents a new line, but here, just > the plain text "\n"!No, this is exactly the same as in C/C++. If you compare the result of sprintf to "Sven is 7.100000 feet tall\n" with strcmp() in C, they will compare equal. > s <- sprintf("%s is %f feet tall\n", "Sven", 7.1) > s [1] "Sven is 7.100000 feet tall\n" > nchar(s) [1] 27 > substr(s,27,27) [1] "\n" The thing that is confusing you is that strings are DISPLAYED using the same escape-character mechanisms as used for input. Compare > cat(s) Sven is 7.100000 feet tall >> > Is it a bug, or a deliberate design?Design, not bug (and please don't file as bug when you are in doubt.)> Thanks for your attention! > > Best wishes! > > Yours sincerely, > Baoli Qin > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
savicky at cs.cas.cz
2009-Dec-21 17:41 UTC
[Rd] a little bug for the function 'sprintf' (PR#14161)
> When you type in the example codes: > > > sprintf("%s is %f feet tall\n", "Sven", 7.1) > > and R returns: > > [1] "Sven is 7.100000 feet tall\n" > > this is very different from the 'sprintf' function in C/C++, for in C/C++, > the format string "\n" usually represents a new line, but here, just > the plain text "\n"!The function sprintf() produces a string, which is then printed using R formatting. You probably expected the following. > cat(sprintf("%s is %f feet tall\n", "Sven", 7.1)) Sven is 7.100000 feet tall Compare also > s <- sprintf("%s is %f feet tall\n", "Sven", 7.1) > cat(s) Sven is 7.100000 feet tall > print(s) [1] "Sven is 7.100000 feet tall\n" > PS.