kableExtra
kableExtra copied to clipboard
Images in table cells
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.
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.
I used the same trick, but it won't work when you want to render to pdf...
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?
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.