3

I am trying to group row names in a R data frame for typesetting with the Hmisc latex() function. Problem is that latex() adds two tilde characters before each row name, and these show up in my document.

How can I either remove these characters or have them not show up?

Example:

test.df <- data.frame(row.names=letters[1:4],   col1=1:4, col2=4:1, col3=4:7)
latex(test.df, file="", n.rgroup=c(2,2), rgroup=c("First","Second"))

Edit: The latex function occurs inside a knitr chunk. The resulting .Rnw file is compiled through the knit2pdf function, which uses pdfLatex by default, I think. All other tables/figures in the document compile fine, without any residual LaTex syntax showing up.

clay
  • 51
  • 3

2 Answers2

2

They will not show up if you use latex with a TeX processor:

test.df <- data.frame(row.names=letters[1:4],   col1=1:4, col2=4:1, col3=4:7)
latex(test.df, file="test", n.rgroup=c(2,2), rgroup=c("First","Second"))

enter image description here

If you want to "capture" the text that is "printed" to the screen and remove the double tildes with sub then you probably need to use capture.output, because it appears that latex is not returning a value but is acting more like the cat function which has output to the screen as a side-effect:

out <- sub("^~~", "", capture.output( 
                     latex(test.df, file="", 
                         n.rgroup=c(2,2), rgroup=c("First","Second")))) 

You could then use writeLines or cat with a file argument to send that text to a destination. I suppose it is possible that you could just put the sub call inline without diverting the results to a named object. That will depend on exactly how your are processing this text.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • The latex function occurs inside a knitr chunk and the resulting .Rnw file is compiled with the knitr knit2pdf function. All other tables/figures in the document compile fine, so I think it is a problem specific to the tildes. (Original question edited with this information). Everything is going through a Tex processor. Thank you for your help. – clay Jul 26 '13 at 13:56
  • The capture.output and sub solution worked perfectly. Thank you. The only remaining question would be: how to indent content in an Hmisc column without using tildes? – clay Jul 29 '13 at 14:14
1

If you don't want to use LaTeX then i suggest either the ascii package that has pretty advanced options that do a nice raw text output (it also has the rgroup & n.rgroup options for grouping row names). If you are interested in getting the tables into a Word document (or just HTML) i suggest Markdown with my htmlTable function - the arguments are based upon the Hmisc latex arguments as I needed a replacement when I was switching to Markdown, thus all you need to do is change the function name to htmlTable after loading my package.

Max Gordon
  • 5,367
  • 2
  • 44
  • 70
  • This project is very much LaTex dependent at this point, but I will check out the ascii package in the future. Thank you for the tip. – clay Jul 26 '13 at 14:12