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!

5
  • 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 Commented Apr 30, 2018 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. Commented Apr 30, 2018 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! Commented Oct 2, 2019 at 7:41
  • Local paths should also work. Can't try this now but as far as I know pdfs should also be fine Commented Oct 2, 2019 at 10:23
  • 1
    Tried various approaches, but the formatting is not as easy, see my post here: stackoverflow.com/questions/58204272/… any help is greatly appreciated! Commented Oct 2, 2019 at 15:09

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
No problem at all :-)
How to resize the image rx4 inside the table and print table without heading?
You can resize the image (with magick or something) and the pander package might have options for printing without the df header.
@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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.