Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a self-contained html report with rmarkdown?

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")
  ) 
```
like image 493
AJMA Avatar asked Mar 16 '18 08:03

AJMA


1 Answers

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")
like image 135
G. Grothendieck Avatar answered Oct 30 '22 07:10

G. Grothendieck