0

I have a dataframe that I want to turn into a table with right alignment in Rmarkdown

---
title: "Test"
output: 
  pdf_document
---

```{r testing, results='asis'}
library(xtable)
df <- structure(list(ID = c(101L, 102L, 103L, 104L, 105L, 106L), 
                       Gr1 = c(10.76,983.4, 34.000, 20, 23.8457, 13.32),
                       Gr2 = c(NA,NA, NA, 20L, NA, NA)
                       ), 
                  .Names = c("ID", "Grade1", "Grade2"), 
                  class = c("tbl_df", "data.frame"), 
                  row.names = c(NA, -6L))
xtable(df,
       align = c('l', 'p{1.5in}', rep('r{0.5in}',2)), 
       digits=c(0,0,1,0))
```

This gives me an error on the rep('r{0.5in}',2)

! LaTeX Error: Illegal character in array arg.

I've also tried: align = c('l|', 'p{1.5in}|', rep('R{0.5in}|',2))

The following works fine: align = c('l', 'p{1.5in}', rep('p{0.5in}',2))

The data isn't right aligned

But the data is left aligned

pluke
  • 3,832
  • 5
  • 45
  • 68
  • I don't get your point: If you want to do the last two columns to the right, then `rep('r',2)`. Only the argument `p` has a width argument. – J_F Oct 21 '16 at 11:54
  • But wouldn't that get rid of the width argument for the last two columns? I need both width and right alignment – pluke Oct 21 '16 at 11:57
  • 1
    `r,l,c` doesn't take width argument by default. You will need to create a custom column type say `R,L,C` to get the desired result which you can find [Here](http://stackoverflow.com/questions/33208777/how-to-set-both-column-width-and-text-alignment-in-align-argument-of-xtable) – 9Heads Oct 21 '16 at 19:00

2 Answers2

1

Here's an approximation of your table. I don't know that I got the widths exactly right. Clearly there's something fishy happening with pixiedust, because when I change width_units from "cm" to "in", I get a table that is much too wide.

---
title: "Test"
output: 
  pdf_document
header-includes: 
- \usepackage{amssymb} 
- \usepackage{arydshln} 
- \usepackage{caption} 
- \usepackage{graphicx} 
- \usepackage{hhline} 
- \usepackage{longtable} 
- \usepackage{multirow} 
- \usepackage[dvipsnames,table]{xcolor} 
---

```{r testing, results='asis'}
library(pixiedust)
df <- structure(list(ID = c(101L, 102L, 103L, 104L, 105L, 106L), 
                       Gr1 = c(10.76,983.4, 34.000, 20, 23.8457, 13.32),
                       Gr2 = c(NA,NA, NA, 20L, NA, NA)
                       ), 
                  .Names = c("ID", "Grade1", "Grade2"), 
                  class = c("tbl_df", "data.frame"), 
                  row.names = c(NA, -6L))
dust(df, keep_rownames = TRUE,
     float = FALSE) %>%
  sprinkle_colnames(.rownames = "") %>%
  medley_bw() %>%
  sprinkle_table(cols = ".rownames",
                 border = "right") %>%
  sprinkle_table(cols = c("Grade1", "Grade2"),
                 halign = "right") %>%
  sprinkle(cols = c("ID", "Grade1", "Grade2"),
           width = c(1.5, 0.05, 0.05),
           width_units = "cm",
           na_string = "",
           recycle = "cols") %>%
  sprinkle(cols = "Grade1",
           fn = quote(sprintf("%.1f", value)))

```
Benjamin
  • 16,897
  • 6
  • 45
  • 65
1

Solved, I have to create two include files to inject the necessary latex. First update the YAML on the markdown file:

---
title: "Test something"
output: 
  pdf_document:
    includes:
      in_header: preamble-latex.tex
      before_body: prebody-latex.tex
---
<SNIP, SEE ABOVE>

Then create preamble-latex.tex to load the array package

\usepackage{array}

And create prebody-latex.tex to store your new column command

\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
pluke
  • 3,832
  • 5
  • 45
  • 68