Question

I am using Knitr in Rstudio, to generate markdown files. I display the tables via xtable package and it shows up nicely in html file. However, when I converd .md to latex via pandoc - the latex file does not contain the tables as it is supposed to be, but only the values in table without any command.

Markdown - Knitr input

In order to give a better idea, the following table provides a sample of data rows:

```{r table, results='asis', echo=FALSE} 
r = read.table("C:/aR_files/data.txt",sep=",", header=TRUE,as.is=TRUE)
r$X = NULL;
print(xtable(r), type='html') 
```

Latex

In order to give a better idea, the following table provides a sample of
data rows:


Row1

Row2

Val1

Val1

I thought I may be missing a latex package, so I downloaded ctable.sty, but still I get the same output. Any ideas appreciated, thanks!

Was it helpful?

Solution

I use a very similar workflow to yours and your best bet is to abandon the often clunky xtable package and use the pander package to print your tables. You can wrap any object that you might want to display as a table in the generic pander() function. This is a wrapper for the pandoc.table() function which has several options. If you specify the option style = "XXX" you can achieve what you are asking about here. There are 4 different styles you can choose from; "multiline" (the default), "grid", "simple", or "rmarkdown". I frequently knit rmarkdown documents from within Rstudio and then convert them to Word documents using the pander package:

library(pander)
Pandoc.convert("C:/Users/BlahBlahBlah/Document.md", format="docx")

All of the 4 table styles get turned into table objects upon conversion to .docx format, but only one table style looks right in the .docx document and the .html file that results from the initial "knit". That style is "rmarkdown". You can implement this 2 ways. One would be:

```{r table, results='asis'}
pandoc.table(myTable, style = "rmarkdown")
```

I prefer to set the table style globally at the beginning of my document however, ensuring that all my tables have the same formatting and also allowing me to use the more succinct pander(x) instead of the more verbose pandoc.table(x, style = "someStyle"):

```{r table, results='asis'}
panderOptions("table.style", "rmarkdown")
pander(myTable)
```

There are some side effects of using the rmarkdown style however. Mainly, it does not support newline characters within cells, so buyer beware. I experimented with the different styles and eventually decided that I liked the default style of "multiline" because of it's flexibility with line breaks within cells, even though the .html files I generate look silly. This doesn't bother me though, as I really only use the .docx files that I convert from the .md files. I wrote a blog post about making nice tables that you might find useful. It weighs the pros and cons of several methods including xtable() and several pander() scenarios.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top