Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R --markdown to latex - tables do not show up

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!

like image 650
Roark Avatar asked Nov 05 '12 00:11

Roark


People also ask

Why is my RMarkdown not knitting?

No Knit HTML button This means that RStudio doesn't understand your document is supposed to be an RMarkdown document, often because your file extension is . txt . To fix this, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document's name.

How do I display LaTeX in Markdown?

LaTeX \newcommand in R Markdown Define the command as you would in LaTeX. Put it below the YAML header at the beginning of your R Markdown file. Call the new command within $… $ to let R Markdown know that this command is defined in the LaTeX environment.

Can I use LaTeX code in RMarkdown?

By default, Pandoc will preserve raw LaTeX code in Markdown documents when converting the document to LaTeX, so you can use LaTeX commands or environments in Markdown.

What is knitr :: Kable?

10.1 The function knitr::kable() The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.


Video Answer


1 Answers

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.

like image 170
rnorberg Avatar answered Nov 24 '22 00:11

rnorberg