kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

Images in table cells

Open prokulski opened this issue 5 years ago • 4 comments

It is possible to add rendering images in cell? For example I put in cell string like "" and in table I get this image.

Workaround for Latex is here https://stackoverflow.com/questions/38555040/alignment-of-images-in-tables-with-markdown-rstudio-and-knitr but it won't work if you'll import knitr and then kableExtra. Without kableExtra it is difficult to change column width etc.

prokulski avatar May 17 '19 12:05 prokulski

Hi Prokulski,

I am not sure if this answers your question but I have achieved this by pre-rendering the image, then adding a new column for the image eg:

table1 %>% mutate(images = paste0("<","img src=","image_file_name",".png",">")) %>% kable(format = "html", escape = F) %>% kable_styling(bootstrap_options = "striped")

If you want specific images for specific rows, it possible by saving the image as the row number eg: 1.png and then having the image_file_name as nrow(table1):

table1 %>% mutate(images = paste0("<","img src=",nrow(table1),".png",">")) %>% kable(format = "html", escape = F) %>% kable_styling(bootstrap_options = "striped")

If it is a static image that does not need to change, you can always:

gsub(pattern = "", x = table1$column1, replacement = paste0("<","img src=","image_file_name",".png",">"))

to replace all instances of "" with the image file name and when you render the table ensure you have format as "html" and escape = F.

ridwan-shaikh avatar Jun 19 '19 10:06 ridwan-shaikh

I used the same trick, but it won't work when you want to render to pdf...

prokulski avatar Jun 19 '19 12:06 prokulski

Never tried with a PDF because most tables lose that better html look. Could you render to html then use chrome_print from the pagedown package to convert the html to pdf document?

ridwan-shaikh avatar Jun 19 '19 13:06 ridwan-shaikh

I do a lot of reports in PDFs (generated via Shiny app, by scripts runned in cron etc) so printing them manual isn't a solution.

prokulski avatar Jun 19 '19 14:06 prokulski