sym246
sym246

Reputation: 1866

Embed image in HTML r markdown document that can be shared

I have an R markdown document which is created using a shiny app, saved as a HTML. I have inserted a logo in the top right hand corner of the output, which has been done using the following code:

<script>
   $(document).ready(function() {
     $head = $('#header');
     $head.prepend('<img src=\"FILEPATH/logo.png\" style=\"float: right;padding-right:10px;height:125px;width:250px\"/>')
   });
</script>

However, when I save the HTML output and share the output, of course the user cannot see the logo since the code is trying to find a file path which will not exist on their computer.

So, my question is - is there a way to include the logo in the output without the use of file paths? Ideally I don't want to upload the image to the web, and change the source to a web address.

Upvotes: 3

Views: 4654

Answers (1)

S&#233;bastien Rochette
S&#233;bastien Rochette

Reputation: 6671

You can encode an image file to a data URI with knitr::image_uri. If you want to add it in your document, you can add the html code produced by the following command in your header instead of your script:

htmltools::img(src = knitr::image_uri("FILEPATH/logo.png"), 
               alt = 'logo', 
               style = 'float: right;padding-right:10px;height:125px;width:250px')

Upvotes: 6

Related Questions