2

The following code generates a PDF file with a table when compiled from R Studio. Is there a way I can insert a double vertical bar (rule) between the variables? This would preferably use pander but I'm not restricted to it.

---
output: 
    pdf_document:
        fig_caption: yes
---

```{r}
pander::pander(cars[1:5,], 
               split.cell = 80, 
               split.table = Inf, 
               digits = 4, 
               caption = "Some Caption\\label{tab:sometable}",
               justify = c('right', 'left'))
```

enter image description here


Edit

I have tried using htmlTable as suggested in answers below. Unfortunately this doesn't create valid markdown code such that knitr can create the PDF e.g.

---
output: 
    pdf_document:
        fig_caption: yes
---

```{r}
library('htmlTable')
htmlTable(as.matrix(cars)[1:5, ], caption = 'Table 1: Some caption.',
          css.table = 'border-collapse: collapse; border-style: hidden; border-bottom: 1px;',
          css.cell = 'border-style: none double none none;')
```

produces: enter image description here

James Owers
  • 7,948
  • 10
  • 55
  • 71
  • Maybe try [htmlTable](https://cran.r-project.org/web/packages/htmlTable/vignettes/tables.html). – zx8754 Mar 15 '16 at 15:18
  • 1
    Unfortunately, there's no markup for vertical/horizontal lines in markdown tables, so if you want to fine-tune the output in PDF, HTML etc, then you have to use stylesheets or templates (eg to tell `pdflatex` which table environment to use, like `ctable` over `longtable` etc). But to keep this short, you should stick with `xtable` or similar packages generating LaTeX or HTML code instead of markdown. – daroczig Mar 15 '16 at 18:39

3 Answers3

3

You might want to try Max Gordon's htmlTable

His example from the vignette:

htmlTable(txtRound(mx, 1), 
          col.columns = c(rep("#E6E6F0", 4),
                          rep("none", ncol(mx) - 4)),
          align="rrrr|r",
          cgroup = cgroup,
          n.cgroup = n.cgroup,
          rgroup = c("First period", 
                     "Second period",
                     "Third period"),
          n.rgroup = rep(5, 3),
                    tfoot = txtMergeLines("&Delta;<sub>int</sub> correspnds to the change since start",
                                "&Delta;<sub>std</sub> corresponds to the change compared to national average"))

Creates

Lines separate Sweeden from the others

Scott
  • 642
  • 7
  • 16
  • this is much more convenient than mine. `htmlTable(as.matrix(cars)[1:5, ], caption = 'Table 1: Some caption.', align = 'c|c')` is all op needs, but this is only a single line – rawr Mar 15 '16 at 15:45
  • I rep'd yours for the double || – Scott Mar 15 '16 at 15:48
  • fwiw i tried `align = 'c||c' ` just now and it gives only single |. so if double line needed, @rawr has it. – Scott Mar 15 '16 at 15:53
  • I can't figure out how to change the css for the "rrrr|r" format without doing some contrived work-around like in mine – rawr Mar 15 '16 at 15:55
  • This solution works in the GUI but not when trying to knit to PDF. Please see my edit. – James Owers Mar 15 '16 at 16:00
  • Sorry. [This](http://stackoverflow.com/questions/33169464/how-to-render-table-from-htmltable-package-in-pdf-document-in-rmarkdown) suggests outputting to html and then using pandoc to convert it to pdf. I am unable to try it but hopefully it helps. – Scott Mar 15 '16 at 16:21
  • Did you try using results='asis' here? – Scott Mar 15 '16 at 17:47
3

For pdf's xtable would be my preferred choice to display tables:

```{r results="asis",echo=FALSE,message=FALSE}
library(xtable)

print(xtable(as.matrix(cars)[1:5, ],align=c("rr||r"), caption="some caption"), include.rownames=FALSE)

```

Produces this output: enter image description here

There are various options available to modify your table: https://cran.r-project.org/web/packages/xtable/xtable.pdf

James Owers
  • 7,948
  • 10
  • 55
  • 71
Sebastian
  • 2,430
  • 4
  • 23
  • 40
  • Does the job. Thank you. Compared with pander it is very ugly! Do you know how to match the pander style? – James Owers Mar 15 '16 at 16:17
  • The code is but when you spend some time with it it will be more readable. Helps if you know latex. Think you have to set it together how you want it.. added a caption to start with for your. – Sebastian Mar 15 '16 at 16:26
  • I meant the output style is ugly, the code is v similar to pander bar `align`. Worth noting here that the key is the chunk option `results="asis"` such that the LaTeX output is written straight to the file. I'll do some documentation digging. Thanks again. – James Owers Mar 15 '16 at 16:33
2

+1 for htmlTable

library('htmlTable')
htmlTable(as.matrix(cars)[1:5, ], caption = 'Table 1: Some caption.',
          css.table = 'border-collapse: collapse; border-style: hidden; border-bottom: 1px;',
          css.cell = 'border-style: none double none none;')

enter image description here

rawr
  • 20,481
  • 4
  • 44
  • 78
  • This solution works in the GUI but not when trying to knit to PDF. Please see my edit. – James Owers Mar 15 '16 at 16:00
  • 1
    @kungfujam true, it's hard to convert html properly to latex. xtable is the better solution – rawr Mar 15 '16 at 16:50
  • 1
    Your response along with @Scott Jacobs above very much appreciated nonetheless. Looks great within the GUI and when generating HTML. Many thanks. – James Owers Mar 15 '16 at 17:05