llewmills
llewmills

Reputation: 3568

Aligning and italicising table column headings using Rmarkdown and pander

I am writing a rmarkdown document knitting to pdf with tables taken from portions of lists from the ezANOVA package. The tables are made using the pander package. Toy Rmarkdown file with toy dataset below.

---
title: "Table Doc"
output: pdf_document
---
```{r global_options, include=FALSE}
#set global knit options  parameters. 
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/', 
                      echo=FALSE, warning=FALSE, message=FALSE, dev = 'pdf')
```


```{r, echo=FALSE}
# toy data
id <- rep(c(1,2,3,4), 5)
group1 <- factor(rep(c("A", "B"), 10))
group2 <- factor(rep(c("A", "B"), each = 10))
dv <- runif(20, min = 0, max = 10)
df <- data.frame(id, group1, group2, dv)
```

``` {r anova, echo = FALSE}
library(ez)
library(plyr)
library(pander)

# create anova object
anOb <- ezANOVA(df,
                dv = dv,
                wid = id,
                between = c(group1, group2),
                type = 3,
                detailed = TRUE)

# extract the output table from the anova object, reduce it down to only desired columns
anOb <- data.frame(anOb[[1]][, c("Effect", "F", "p", "p<.05")])

# format entries in columns
anOb[,2] <- format( round (anOb[,2], digits = 1), nsmall = 1)
anOb[,3] <- format( round (anOb[,3], digits = 4), nsmall = 1)

pander(anOb, justify = c("left", "center", "center", "right"))
```

Now I have a few problems

a) For the last three columns I would like to have the column heading in the table aligned in the center, but the actual column entries underneath those headings aligned to the right.

b) I would like to have the column headings 'F' and 'p' in italics and the 'p' in the 'p<.05' column in italics also but the rest in normal font. So they read F, p and p<.05

I tried renaming the column headings using plyr::rename like so

anOb <- rename(anOb, c("F" = "italic(F)", "p" = "italic(p)", "p<.05" = ""))

But it didn't work

Upvotes: 1

Views: 1075

Answers (1)

daroczig
daroczig

Reputation: 28632

In markdown, you have to use the markdown syntax for italics, which is wrapping text between a star or underscore:

> names(anOb) <- c('Effect', '*F*', '*p*', '*p<.05*')
> pander(anOb)

-----------------------------------------
    Effect       *F*     *p*     *p<.05* 
--------------- ------ -------- ---------
  (Intercept)    52.3   0.0019      *    

    group1       1.3    0.3180           

    group2       2.0    0.2261           

 group1:group2   3.7    0.1273           
-----------------------------------------

If you want to do that in a programmatic way, you can also use the pandoc.emphasis helper function to add the starts to a string.

But your other problem is due to a bug in the package, for which I've just proposed a fix on GH. Please feel free to give a try to that branch and report back on GH -- I will try to get some time later this week to clean up the related unit tests and merge the branch if everything seem to be OK.

Upvotes: 1

Related Questions