7

How can I produce an html document from a .Rmd file, with self-contained images? I am using the bsplus package along with rmarkdown to create a carousel of images. It works perfectly when I open the .html output within the .Rproj working directory, but the images are no longer displayed when I send the file to someone.

Would it be possible to get a "self-contained" .html file output with the corresponding images? or should I send also all folder dependencies?

Example of how the code looks like...

---
title: "test"
author: "me"
date: "today"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r carousel}
bs_carousel(id = "the_beatles", use_indicators = TRUE) %>%
  bs_append(
    content = bs_carousel_image(src = image_uri("img/john.jpg")),
    caption = bs_carousel_caption("John Lennon", "Rhythm guitar, vocals")
  ) %>%
  bs_append(
    content = bs_carousel_image(src = image_uri("img/paul.jpg")),
    caption = bs_carousel_caption("Paul McCartney", "Bass guitar, vocals")
  ) %>%
  bs_append(
    content = bs_carousel_image(src = image_uri("img/george.jpg")),
    caption = bs_carousel_caption("George Harrison", "Lead guitar, vocals")
  ) %>%
  bs_append(
    content = bs_carousel_image(src = image_uri("img/ringo.jpg")),
    caption = bs_carousel_caption("Ringo Starr", "Drums, vocals")
  ) 
```
AJMA
  • 1,134
  • 2
  • 13
  • 28

3 Answers3

7

Assuming that the file shown in the question is in the current directory and called caro.Rmd and the *.jpg files are all present at the appropriate location and you are able to run pandoc then this works for me:

library(knitr)
library(rmarkdown)

render("caro.Rmd", html_document(pandoc_args = "--self-contained"))
browseURL("caro.html")
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0
To post the report on sharepoint:
    setwd("C:/Users/...")
#render table
    rmarkdown::render("tablename.rmd") #run the rmd file in the extracts folder
    file.remove("tablename.aspx") #remove the current aspx sharepoint from previous render
    file.rename("tablename.html",`enter code here`"tablename.aspx") #rename the html to be the new one
-1

This is not an R/Rmarkdown problem - it's an HTML one. Most HTML files read images from files, not from within the code.

If you absolutely need to do this, see this question and/or this website.

However, I would instead suggest gzipping the directory in which images are stored + your .html file, and sending that archive via email.

dvanic
  • 545
  • 1
  • 4
  • 17