Dear R community,
is there any way to include a backslash in a charakter string without meaning some escape sequence?
E.g. i need a string like:
a <- '\hline'
Error: '\h' is an unrecognized escape in character string starting "\h"
to include some latex code in a xtable object, but I only mange to do:
a <- '\\hline'
which is not what I want.
In detail I would like to add the following line of code to the first row of an xtable object with the add.to.row argument:
& \multicolumn{4}{c}{A} \multicolumn{4}{c}{B} \\
for which I have to convert it into a character sting.
Any ideas?
Jannis
Backslash \ in string
4 messages · Erik Iverson, Jannis, Claudia Beleites
On 07/15/2010 08:02 AM, Jannis wrote:
Dear R community, is there any way to include a backslash in a charakter string without meaning some escape sequence? E.g. i need a string like: a<- '\hline' Error: '\h' is an unrecognized escape in character string starting "\h" to include some latex code in a xtable object, but I only mange to do: a<- '\\hline' which is not what I want.
Why don't you want that? I use something like that every day with LaTeX and it works fine, so I'm not sure what you mean. I don't use xtable though, so I can't speak to that. But can you explain what happens in the resulting .tex file when you use '\\'. Escaping each backslash you want in your .tex file with another should be the way to go, e.g., the table end of line would be written "\\\\" in R.
In detail I would like to add the following line of code to the first row of an xtable object with the add.to.row argument:
& \multicolumn{4}{c}{A} \multicolumn{4}{c}{B} \\
for which I have to convert it into a character sting.
Thanks for your reply Erik! You are right, it does not seem to matter. When the R string contains two \\, xtable prints it as only one \. I should have looked into the Latex output before posting! Thanks again, and sorry for posting too quick! Jannis --- Erik Iverson <eriki at ccbr.umn.edu> schrieb am Do, 15.7.2010:
Von: Erik Iverson <eriki at ccbr.umn.edu> Betreff: Re: [R] Backslash \ in string An: "Jannis" <bt_jannis at yahoo.de> CC: r-help at r-project.org Datum: Donnerstag, 15. Juli, 2010 13:09 Uhr On 07/15/2010 08:02 AM, Jannis wrote:
Dear R community, is there any way to include a backslash in a charakter
string without meaning some escape sequence?
E.g. i need a string like: a<- '\hline' Error: '\h' is an unrecognized escape in character
string starting "\h"
to include some latex code in a xtable object, but I
only mange to do:
a<- '\\hline' which is not what I want.
Why don't you want that?? I use something like that every day with LaTeX and it works fine, so I'm not sure what you mean.? I don't use xtable though, so I can't speak to that.? But can you explain what happens in the resulting .tex file when you use '\\'.? Escaping each backslash you want in your .tex file with another should be the way to go, e.g., the table end of line would be written "\\\\" in R.
In detail I would like to add the following line of
code to the first row of an xtable object with the add.to.row argument:
&? \multicolumn{4}{c}{A}
\multicolumn{4}{c}{B} \\
for which I have to convert it into a character
sting.
Jannis,
You are right, it does not seem to matter. When the R string contains two \\, xtable prints it as only one \. I should have looked into the Latex output before posting!
'\\' is just _one_ character in R:
> nchar ("\\")
[1] 1
Just like '\n' etc.
It is just the `print`ed (as opposed to cat) output that mislead you:
the print function displays a bunch of special characters in their
backslash-escaped fashion:
> print ("someting\tblah\\blubb\n")
[1] "someting\tblah\\blubb\n"
> cat ("someting\tblah\\blubb\n")
someting blah\blubb
> print ("\12")
[1] "\n"
Claudia