10

I have a simple data structure: cases are countries and for each country I have a couple of numeric variables. Like so:

dat <- data.frame(country = c("Belgium", "Germany", "Holland", "Ireland"), Var1 = 1:4, Var2 = 11:14)
print(dat, row.names = FALSE)

 country Var1 Var2
 Belgium    1   11
 Germany    2   12
 Holland    3   13
 Ireland    4   14

The table needs to be formatted still, with headings in bold, and rows colored in grey or white, alternatingly.

Now, what I want is to add two additional columns, in between "country" and "Var1". The first new column is called "flag" and should contain the country's flag. The second new column is called "flagged" and contains an image of a red flag is the country scores very bad on a particular human rights issue, an orange flag if it scores mediocre and nothing elsewise.

How can I create an object that prints that way in R? How to combine images with data in such a layout?

(eventually this will part of a larger document created with knitr)

Andrie
  • 176,377
  • 47
  • 447
  • 496
Peter Verbeet
  • 1,786
  • 2
  • 13
  • 29

3 Answers3

24

If you are using knitr with the rmarkdown package, it is pretty easy -- just use the Markdown syntax ![]() to include images, e.g.

---
title: "Flags"
author: "Yihui Xie"
date: "2014/08/03"
output: html_document
---

```{r results='asis'}
dat <- data.frame(
  country = c('Canada', 'United Kindom'),
  abbr = c('ca', 'gb'),
  var1 = c(1, 2),
  var2 = rnorm(2)
)
dat$flag <- sprintf('![](http://flagpedia.net/data/flags/mini/%s.png)', dat$abbr)
library(knitr)
kable(dat)
```

knitr, tables, and flags

If you need LaTeX/PDF output, you have to download these images by yourself. Here is an example:

---
title: "Flags"
author: "Yihui Xie"
date: "2014/08/03"
output: html_document
---

```{r}
dat <- data.frame(
  country = c('Canada', 'United Kindom'),
  abbr = c('ca', 'gb'),
  var1 = c(1, 2),
  var2 = rnorm(2)
)
dat$file <- paste0(dat$abbr, '.png')
dat$link <- paste0('http://flagpedia.net/data/flags/mini/', dat$file)
dat$flag <- sprintf('![](%s)', dat$file)
for (i in seq_len(nrow(dat))) {
  if (!file.exists(dat$file[i])) xfun::download_file(dat$link[i])
}
knitr::kable(dat[, -c(5, 6)])
```
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • That's really nice. I indeed just started working with knitr to be able to create the report that this table will be part of. I hadn't figured out this solution yet, and was about to start writing code in R HTML to make this happen. But your solution is quite straightforward. Would there be an advantage to going the R HTML route for this? – Peter Verbeet Aug 03 '14 at 19:55
  • 1
    @PeterVerbeet HTML is a lot more fun than either Word or LaTeX. If you take a look at the rmarkdown link I included in the answer, you will realize the output of R Markdown is not necessarily HTML. – Yihui Xie Aug 06 '14 at 02:20
  • Is there a way to do the same thing, while using Latex instead of rmarkdown? I have tried with "\includegraphics" as part of dat$flag, but it breaks down no matter how I try. – Peter Verbeet Aug 22 '14 at 20:20
  • @PeterVerbeet There may be a couple of issues: 1. you need `\\includegraphics` (two backslashes); 2. the flags must exist locally (http links won't work in LaTeX). – Yihui Xie Aug 22 '14 at 20:29
  • Thanks, that works, I really appreciate your guidance. Can I pass additional layout options to kable? I would like to make text in the top row bold and would like to fill the rows alternatingly with white and grey. Is that possible? – Peter Verbeet Aug 23 '14 at 08:45
  • @PeterVerbeet I believe you can find a LaTeX package to do that, instead of doing it in R. – Yihui Xie Aug 23 '14 at 15:47
  • @PeterVerbeet Could you provide the code to insert images in pfd-tables, using include_graphics()? – MarkusN Aug 15 '17 at 15:08
  • It might be worth looking at the `pixiedust` package for background graphics for tables within R. –  Nov 17 '17 at 17:40
  • @YihuiXie: when I use your code to compile a PDF instead of HTML, I get the following error: `! Package pdftex.def Error: File "http://flagpedia.net/data/flags/mini/ca.png" not found: using draft setting.`. Is there a way to circumvent this to generate a PDF version? Many thanks in advance! – mavericks Oct 02 '19 at 09:58
  • See also my related question here: https://stackoverflow.com/questions/58204272/r-markdown-how-to-create-a-table-with-images-and-text-which-should-be-knitted-a – mavericks Oct 02 '19 at 15:08
  • 1
    @mavericks I have updated my answer for PDF output. You need to download the images. – Yihui Xie Oct 02 '19 at 16:30
  • @YihuiXie, thank you for your quick response and the reprex, which works well in a simple markdown file. However, in a PDF compiled with bookdown, it simply shows `![](ca.png)` instead of the flag in the table. Would you have any recommendations on how to make it work there too? – mavericks Oct 03 '19 at 12:50
  • 1
    @mavericks Use `kable(..., format = 'pandoc')` if you are using bookdown. – Yihui Xie Oct 03 '19 at 14:23
  • Thank you @YihuiXie, that works well! Would you also have any recommendations on how to improve the markdown table or the kable/kableExtra table approach at my SO question here https://stackoverflow.com/questions/58204272/r-markdown-how-to-create-a-table-with-images-and-text-which-should-be-knitted-a ? Many thanks in advance! – mavericks Oct 03 '19 at 18:56
3

With this experimental fork of gtable, you can do,

require(gtable)

dat <- data.frame(country = c("Belgium", "Germany", "Holland", "Ireland"), Var1 = 1:4, Var2 = 11:14)
g <- gtable_table(dat)

library(png)
# pirate-land flag for illustration
img <- readPNG(system.file("img", "Rlogo.png", package="png"), native = FALSE)
imgRgb <- rgb(img[,,1],img[,,2],img[,,3])
dim(imgRgb)  <- dim(img)[1:2]
flags <- replicate(nrow(g), rasterGrob(imgRgb), simplify = FALSE)
g <- gtable_add_cols(g, unit(1,"cm"), 0)
g <- gtable_add_grob(g, flags, t = seq_len(nrow(g)), l=1, r=1, z=1)

grid.newpage()
grid.draw(g)

formatting options described here

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Hi Baptiste, this is an intriguing option. It is certainly easy to add formatting to the table like this (such as alternatingly adding a lightgrey background to the rows). I will have to study this further, thanks. – Peter Verbeet Aug 03 '14 at 20:55
1

The question was asked with pdf as the output, here is an answer using knitr, and something more appropriate than just \includegraphics

Table with flags

The trick is to use the adjustbox package in latex, with the following arguments:

  • the height (later used as the argument to the R function get_picture_code) is the height of the picture.
  • the valign argument (which default here to valign=m), will perform vertical adjustment according to the text,
  • the margin here defined as 1ex surrounding the picture allows to separate the flags.

So we just use this function

get_picture_code <- function(path,height,col=NULL)
{
  paste0("\\adjustimage{height=",height,",valign=m,margin*=1ex}{",path,"}")  
} 

To get a vector of images added to the table.


Finally we use xtable with argument sanitize.text.function = identity to print the tex code :

\documentclass{article}
\usepackage{adjustbox}
\begin{document}
<<load_libraries, echo = FALSE, eval = TRUE, results ="hide">>=
library(knitr) 
library(xtable)
@

<<include_images, echo = FALSE, eval = TRUE, results ="hide">>=
get_picture_code <- function(path,height,col=NULL){
  paste0("\\adjustimage{height=",height,",valign=m,margin*=1ex}{",path,"}")  
} 
@

<<test, echo = FALSE, eval = TRUE, results ="hide">>=
dat <- data.frame(country = c("Belgium", "Germany", "Holland", "Ireland"), 
Var1 = 1:4, Var2 = 11:14) 
mypath <- paste0("images/",dat$country,".png")
dat$flag <- get_picture_code(path=mypath,height="0.8cm")
dat$test <-NA
dat$test[2:3] <-get_picture_code(path="images/orange_flag",height="0.6cm")
print(xtable(dat, 
        align = c("l","l","l","l","c","c"),
        caption = "Example with flags"), 
    sanitize.text.function = identity, 
    file="table_with_images.tex")
@

\input{table_with_images.tex}
\end{document}

The adjustbox documentation contains many other options, including background colors, trim, horizonal alignment which will allow you to do some very fine adjustment of the position of the images... There is also a nice example of the use of this package in TeX-Latex stackexchange

Community
  • 1
  • 1
Cedric
  • 2,412
  • 17
  • 31