Edwin Helbert Aponte Angarita
2012-Jul-30 17:29 UTC
[R] sprintf doesn't care of escape characters
Hi. I am having trouble with something that should be simple. I am unable to get sprintf using escape sequences:> sprintf("a\nb")[1] "a\nb"> sprintf("a\"bc\"d")[1] "a\"bc\"d" But it seems to need them any way:> sprintf("a\"bc"d")Error: unexpected symbol in "sprintf("a\"bc"d" Any suggestion on how to solve this issue? My R system:> version_ platform x86_64-suse-linux-gnu arch x86_64 os linux-gnu system x86_64, linux-gnu status major 2 minor 15.0 year 2012 month 03 day 30 svn rev 58871 language R version.string R version 2.15.0 (2012-03-30) Thanks.
sprintf is working just fine. Your problem is the interpretation of what the results are. Displaying the object will show the escape characters, but if you use "cat", or output to a file, you will see that the result is correct:> cat( sprintf("a\nb"))a b>> cat(sprintf("a\"bc\"d"))a"bc"d>>On Mon, Jul 30, 2012 at 1:29 PM, Edwin Helbert Aponte Angarita <helbert2a at gmail.com> wrote:> Hi. > > I am having trouble with something that should be simple. I am unable > to get sprintf using escape sequences: > >> sprintf("a\nb") > [1] "a\nb" > >> sprintf("a\"bc\"d") > [1] "a\"bc\"d" > > But it seems to need them any way: > >> sprintf("a\"bc"d") > Error: unexpected symbol in "sprintf("a\"bc"d" > > Any suggestion on how to solve this issue? > > My R system: >> version > _ > platform x86_64-suse-linux-gnu > arch x86_64 > os linux-gnu > system x86_64, linux-gnu > status > major 2 > minor 15.0 > year 2012 > month 03 > day 30 > svn rev 58871 > language R > version.string R version 2.15.0 (2012-03-30) > > Thanks. > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
I am unable to determine what you think the problem is from your description. A wild guess is that you just don't know what you are looking at in the output. Do you understand the difference between the output of the "print" function and of the "cat" function? Try cat( sprintf("a\"bc\"d") ) and print ( sprintf("a\"bc\"d") ) In a nutshell, print makes the string suitable for inclusion in source code, while cat sends the string directly as-is. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Edwin Helbert Aponte Angarita <helbert2a at gmail.com> wrote:>Hi. > >I am having trouble with something that should be simple. I am unable >to get sprintf using escape sequences: > >> sprintf("a\nb") >[1] "a\nb" > >> sprintf("a\"bc\"d") >[1] "a\"bc\"d" > >But it seems to need them any way: > >> sprintf("a\"bc"d") >Error: unexpected symbol in "sprintf("a\"bc"d" > >Any suggestion on how to solve this issue? > >My R system: >> version > _ >platform x86_64-suse-linux-gnu >arch x86_64 >os linux-gnu >system x86_64, linux-gnu >status >major 2 >minor 15.0 >year 2012 >month 03 >day 30 >svn rev 58871 >language R >version.string R version 2.15.0 (2012-03-30) > >Thanks. > >______________________________________________ >R-help at r-project.org mailing list >stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.