3

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!

Will Park
  • 55
  • 2
  • 7
  • You should put the image in a table written as markdown, instead of inside a code block. Run `kable`, copy its output and put it into the document not inside a code block – Calum You Apr 30 '18 at 22:43
  • 3
    @CalumYou noooo! the whole point of kable is to be able to return tables from code blocks. He just needs to properly format his links to display images. try `data.frame(a = 1:3, image = paste0('![](http://oganm.com/api/t2i?t=',1:3,')')) %>% knitr::kable()` and keep things in a codeblock. – OganM Apr 30 '18 at 23:33
  • 1
    @OganM: How about if I would like to use local images (PNGs)? Would you know of a solution that works in PDF output? Many Thanks in advance! – mavericks Oct 02 '19 at 07:41
  • Local paths should also work. Can't try this now but as far as I know pdfs should also be fine – OganM Oct 02 '19 at 10:23
  • 1
    Tried various approaches, but the formatting is not as easy, see my post here: https://stackoverflow.com/questions/58204272/r-markdown-how-to-create-a-table-with-images-and-text-which-should-be-knitted-a any help is greatly appreciated! – mavericks Oct 02 '19 at 15:09

1 Answers1

4

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

RobertMyles
  • 2,673
  • 3
  • 30
  • 45
  • 1
    I know you answered this a long time ago but please forgive my ignorance and I hope you would accept my apologies, I just saw this for some reason! Thank you so much for your time and help. – Will Park May 12 '20 at 05:50
  • No problem at all :-) – RobertMyles May 14 '20 at 13:23
  • How to resize the image rx4 inside the table and print table without heading? – Mohamed Jan 04 '21 at 00:57
  • You can resize the image (with magick or something) and the pander package might have options for printing without the df header. – RobertMyles Jan 07 '21 at 15:02
  • @RobertMyles is it still working for you? I am trying, but in HTML it return: Quitting from lines 11-26 (teste_html.Rmd) Error in tableExpand_cpp(cells, cols.width, justify, sep.cols, style) : function ‘Rcpp_precious_remove’ not provided by package ‘Rcpp’ Calls: … pandoc.table.return -> paste -> table.expand -> tableExpand_cpp – fvfaleiro Aug 07 '21 at 23:18