2
votes

I've found the package condformat, that easily creates coloured tables from data.frames.

You can run one of its examples.

data(iris)
library(condformat)
condformat(iris[c(1:5,70:75, 120:125),]) +
  rule_fill_discrete(Species) + 
  rule_fill_discrete(Sepal.Width, Sepal.Length,expression = Sepal.Width > Sepal.Length - 2.25, colours = c("TRUE" = "#7D00FF")) + rule_fill_gradient2(Petal.Length)

enter image description here

I can use it within a markdown document if I generate an html.

But I'm not able to use it to generate a pdf. I've tried both with knitr and with rmarkdown. Knitr produces the error:

Overfull \hbox (22.4968pt too wide) in paragraph at lines 62--62
Missing $ inserted.

and many

You can't use `macro parameter character #' in math mode.

And when I try to embed the code within a rmarkdown chunk it doesn't produce any error but the output it's just a single column with many numbers, without colours.

I've tried both with results='asis' and without it.

How can I get these tables when creating a pdf with a Rnw Knitr?

Sometimes I get similar results with xtable or or ztable but the former is very difficult if you want to selectively colour cells, and the latter doesn't work well sometimes. For example I'm not able to print numbers in scientific format inside a ztable table.

1

1 Answers

2
votes

I think the reason you're having a hard time getting condformat to print to PDF is because it doesn't support PDF output. It only supports HTML output (unless I'm not seeing something in the manual).

So if you'll forgive some shameless self-promotion, I'll recommend the pixiedust package to you. The downside is that pixiedust doesn't have some of the neat features, like automatic color selection or gradients that condformat has, but those are things that are going on my bucket list right now.

The upside of pixiedust is that it supports both HTML and LaTeX/PDF output using the same syntax. The RMarkdown script below will produce a table similar to the example from condformat, and you can switch between Knit PDF and Knit HTML to get the same presentation.

---
title: "Untitled"
output: pdf_document
date: "May 6, 2016"
header-includes: 
- \usepackage{amssymb} 
- \usepackage{arydshln} 
- \usepackage{caption} 
- \usepackage{graphicx} 
- \usepackage{hhline} 
- \usepackage{longtable} 
- \usepackage{multirow} 
- \usepackage[dvipsnames,table]{xcolor} 
- \makeatletter 
- \newcommand*\vdashline{\rotatebox[origin=c]{90}{\$\dabar@\dabar@\dabar@\$}} 
- \makeatother
---

```{r}
library(pixiedust)
library(RColorBrewer)
data(iris)

output <- iris[c(1:5,70:75, 120:125),]

dust(output) %>%
  sprinkle_table(pad = 3) %>%
  sprinkle(
    cols = "Species",
    bg = c(rep("rgb(249,118,110)", sum(output$Species == "setosa")),
           rep("rgb(1,186,56)", sum(output$Species == "versicolor")),
           rep("rgb(98,156,255)", sum(output$Species == "virginica"))),
    recycle = "rows"
  ) %>%
  sprinkle(
    cols = c("Sepal.Length", "Sepal.Width"),
    rows = which(with(output, Sepal.Width > Sepal.Length - 2.25)),
    bg = "#7D00FF"
  ) %>%
  sprinkle(
    cols = "Petal.Length",
    bg = cut(output$Petal.Length, breaks = 9,
             labels = brewer.pal(9, "BrBG")) %>%
           as.character(),
    recycle = "rows"
  ) %>%
  medley_bw()
```