Search code examples
htmlrr-markdownrstudio

Include image in html using R markdown


I have an image named icon.png that I want to put in the title of an html created through RStudio. I got it using this script:

<script>
   $(document).ready(function() {
     $head = $('#header');
     $head.prepend('<img src=\"Icon.png\" style=\"float: left;width: 150px;\"/>')
   });
</script>

My question is related with the path of the image, I just got it putting the file in the same folder than the .Rmd...but I'd like to load the image from a folder called assets in the same folder than thr -Rmd

  • EDA -- assets
  • Icon.png
  • file.Rmd

Thank you in advanced for your help


Solution

  • Option 1:

    You change the img src Tag in your html script.

    like below

    <script>
       $(document).ready(function() {
         $head = $('#header');
         $head.prepend('<img src=\"assets/Icon.png\" style=\"float: left;width: 150px;\"/>')
       });
    </script>
    

    Note That with HTML you can't reference image files outside the domain root (web root) see here https://webmasters.stackexchange.com/a/48448

    Option 2:

    You use Markdown syntax, Note here you can reference image files outside the .rmd file folder

    ---
    title: my_document_title ![](../asset_out/Icon.png){width=1in} 
    output: html_document
    date: "2023-05-09"
    ---