简体   繁体   中英

Include image in data frame R

I have a data frame in R, something like this

       Score   Result
1       1       Pass       
2       0.8     Pass       
3       1       Pass       
4       0.5     Fail        
5       0.3     Fail        
6       0.2     Fail

Depending upon the value of Result (pass or fail), I would like to display thumbs-up or thumbs-down image when I write the data frame to the PDF.

I don't want to use knitr or shiny. Any help would be appreciated.

you could define your own cell function in grid.table,

在此处输入图片说明

d <- read.table(textConnection("Score   Result
1       1       Pass       
2       0.8     Pass       
3       1       Pass       
4       0.5     Fail        
5       0.3     Fail        
6       0.2     Fail"), stringsAsFactors=FALSE)

library(gridExtra)
library(grid)
library(png)
up <- rasterGrob(readPNG("up.png"), height = unit(1,"line"))
down <- rasterGrob(readPNG("down.png"), height = unit(1,"line"))

cell_grob <- function (label, parse = FALSE, col = "black", fontsize = 12, ...) 
{
  switch(label, "Pass" = up, "Fail" = down, 
         grid::textGrob(label = label, ...))

}

custom <- ttheme_default(core = list(fg_fun = cell_grob))
grid.newpage()
grid.table(d, theme = custom)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM