Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple manual RMarkdown tables that look good in HTML, PDF and DOCX

How can I manually and simply format a table in RMarkdown that will look good when converted to HTML (using the knitr and markdown packages), PDF (using pandoc and miktex) and docx (using pandoc)?

I want to be able to write small tables in RMarkdown that are not a result of R functions that look good in the three formats I use most often. So far I've found a format that looks good in 2 of the 3 formats, is 3/3 possible?

One. This looks good after Knit HTML but not good in the PDF or docx

<table> <tr> <td>Eggs</td> <td>Ham</td> </tr> <tr> <td>Basil</td> <td>Tomato</td> </tr> </table> 

Two. This one looks good after Knit HTML but not good in the PDF or docx

| Tables        | Are           | Cool  | | ------------- |:-------------:| -----:| | col 3 is      | right-aligned | $1600 | | col 2 is      | centered      |   $12 | | zebra stripes | are neat      |    $1 | 

Three. This one does not look good after Knit HTML but is good in the PDF and docx (best option so far)

V1         Tweedledee       Tweedledum --------   --------------   ---------------- Age        14               14 Height     3'2"             3'2" Politics   Conservative     Conservative Religion   "New Age"        Syrian Orthodox ---------  --------------   ---------------- 

Four. This looks good after Knit HTML and make PDF and docx (winner!) but is not the manual formatting I'm after.

```{r table1, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'} require(pander) panderOptions('table.split.table', Inf) set.caption("Data on cars") pander(mtcars, style = 'rmarkdown') ``` 

This is how I'm making the PDF and docx files:

filen <- "table" # name of my RMarkdown file without suffix knit(paste0(filen,".Rmd"))  # make PDF system(paste0("pandoc -s ", paste0(filen,".md"), " -t latex -o ", paste0(filen,".pdf"), " --highlight-style=tango  -S"))  # make docx system(paste0("pandoc -s ", paste0(filen,".md"), " -o ", paste0(filen,".docx"), " --highlight-style=tango  -S")) 
like image 322
Ben Avatar asked Nov 15 '13 09:11

Ben


People also ask

Can you insert a table into R markdown?

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.

How do you RMarkdown a PDF?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

What is the difference between markdown and RMarkdown?

R Markdown (markup language)R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.


1 Answers

Inspired by daroczig's comments, especially the clue that pander translates to pandoc's pipe syntax, I took a closer look at the pander documentation and found reference to cat. After some experimentation, I found the winner:

```{r table2, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'} tabl <- " | Tables        | Are           | Cool  | |---------------|:-------------:|------:| | col 3 is      | right-aligned | $1600 | | col 2 is      | centered      |   $12 | | zebra stripes | are neat      |    $1 | " cat(tabl) # output the table in a format good for HTML/PDF/docx conversion ``` 

This produces uniformly good looking tables in HTML, PDF and docx in my tests. Now I'm off to upvote daroczig on some other questions to thank him for getting me to the solution.

If you need a caption for your table... then you'll need to do it a bit differently. Note that the caption will only be visible in the PDF, not in the HTML:

```{r table-simple, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'} require(pander) panderOptions('table.split.table', Inf) set.caption("My great data") my.data <- " # replace the text below with your table data   Tables        | Are           | Cool   col 3 is      | right-aligned | $1600    col 2 is      | centered      |   $12    zebra stripes | are neat      |    $1" df <- read.delim(textConnection(my.data),header=FALSE,sep="|",strip.white=TRUE,stringsAsFactors=FALSE) names(df) <- unname(as.list(df[1,])) # put headers on df <- df[-1,] # remove first row row.names(df)<-NULL pander(df, style = 'rmarkdown') ``` 
like image 118
Ben Avatar answered Sep 24 '22 09:09

Ben