Will Park
Will Park

Reputation: 75

How To Insert Images Into Table in R Markdown

so I have a data frame

employee <- c('John Doe','Peter Gynn','Jolie Hope')
pic_url <- c('url_Johns Picture', 'url_Peters Picture', 'url_Jolies Picture')

df <- data.frame(employee, pic_url)

which looks like this

       employee         pic_url

1    John Doe       url_Johns Picture
2    Peter Gynn     url_Peters Picture
3    Jolie Hope     url_Jolies Picture

When I try to knit an HTML based on this table, I'd first source the R file in a Rmd file and create a table using

 source(myrfile.R)
 df %>% kable()

But this gives me error and after a long struggle, I figured it is because image url links should not be in code blocks. But I cannot think of how to include images inside my table without using code block. I am learning how to use knitr so I would really appreciate it if you guys can show me a way using knitr or basic R. Thank you in advance!

Upvotes: 3

Views: 3845

Answers (1)

RobertMyles
RobertMyles

Reputation: 2832

You can do this with the knitr and pander packages. Car picture taken from here: https://car-from-uk.com/sale.php?id=55162&country=us; renamed to "rx4.jpg" in my working directory.

Code chunk in rmarkdown doc:

library(knitr)
library(dplyr)
library(pander)

mtcars %>% 
  slice(1) %>% 
  mutate(
    pic = "rx4.jpg" %>% pander::pandoc.image.return()
    ) %>% 
  pander()

Produces this output:

enter image description here

Upvotes: 4

Related Questions