Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you color the cell in rmarkdown pdf output

I cannot get cellcolor to work in rmarkdown:

---
header-includes:
   -  \usepackage{colortbl} 
   -  \usepackage{color} 

output:
    pdf_document
---

```{r, results="asis"}

library(xtable)
# Your data
tab = data.frame(category = c("A","B","C"), groupA = c(.2,.3,.5), groupB= c(.6,.7,.9))

# Function to cut your data, and assign colour to each range
f <- function(x) cut(x, c(0, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, Inf), 
                      labels=c("green", "red", "blue", "orange", "yellow", "purple", "brown", "white"),
                      include.lowest = FALSE, right = TRUE)

# Apply function to columns: this overwrites your data
tab[c("groupA", "groupB")] <- lapply(tab[c("groupA", "groupB")], function(x)
                                            paste0("\\cellcolor{", f(x), "}", x))
# Sanitise output 
print(xtable(tab), sanitize.text.function = identity)
```

I keep getting this error:

! Undefined control sequence.
l.155 1 & A & \cellcolor

Any ideas what does cellcolor needs to work?

like image 704
user1471980 Avatar asked Jul 25 '17 19:07

user1471980


People also ask

How do I add color in R markdown?

The Markdown syntax has no built-in method for changing text colors. We can use HTML and LaTeX syntax to change the formatting of words: For HTML, we can wrap the text in the <span> tag and set color with CSS, e.g., <span style="color: red;">text</span> . For PDF, we can use the LaTeX command \textcolor{}{} .

What is the format for including a link that appears as blue text in your Markdown document?

Hyperlinks are created using the syntax [text](link) , e.g., [RStudio](https://www.rstudio.com) .


1 Answers

Update

The OP's example works for me when including \usepackage[dvipsnames]{xcolor} instead. For another approach see below.

Alternative Approach

Here is another approach using the very handy condformat package. This way you don't have to include any TeX packages by hand and don't have to worry about escaping special characters etc.


We basically create a condformat_tbl object and add two formatting rules for each column.

---
output:
    pdf_document
---

```{r, include = F}
library(condformat)
cols        <- c('green', 'red', 'blue', 'orange', 
                 'yellow', 'purple', 'brown', 'white')
names(cols) <- cols # important for the colours arg in rule_fill_discrete
                    # since it matches the result of 'expression' with the names of the 'colours' vector

f   <- function(x) cut(x, c(0, seq(.2, .8, .1), Inf), labels = cols,
                       include.lowest = FALSE, right = TRUE)
tab <- data.frame(category = c('A', 'B', 'C'), 
                  groupA = c(.2, .3, .5), 
                  groupB = c(.6, .7, .9))
```


```{r, results = 'asis', echo = F}
condformat(tab) + 
  rule_fill_discrete(groupA, expression = f(groupA), colours = cols) +
  rule_fill_discrete(groupB, expression = f(groupB), colours = cols) 
```

Notice, that the value for the colours argument in rule_fill_discrete is vector of key-value pairs. The keys are the possible results of the expression.

And this is what you get:

enter image description here

like image 81
Martin Schmelzer Avatar answered Sep 20 '22 22:09

Martin Schmelzer