如何使用rmarkdown创建自包含的html报告?

时间:2018-03-16 08:32:14

标签: r knitr r-markdown

如何使用自包含图像从.Rmd文件生成html文档?我正在使用bsplus包和rmarkdown来创建图像轮播。当我打开.html工作目录中的.Rproj输出时,它可以正常工作,但是当我将文件发送给某人时,图像不再显示。

是否可以使用相应的图像获得“自包含”的.html文件输出?或者我是否应该发送所有文件夹依赖项?

代码的示例......

---
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")
  ) 
```

3 个答案:

答案 0 :(得分:2)

假设问题中显示的文件位于当前目录中并且名为caro.Rmd并且*.jpg文件都存在于适当的位置,并且您可以运行pandoc,那么这对我有效:

library(knitr)
library(rmarkdown)

render("caro.Rmd", html_document(pandoc_args = "--self-contained"))
browseURL("caro.html")

答案 1 :(得分:1)

这不是R / Rmarkdown问题 - 它是一个HTML。大多数HTML文件从文件中读取图像,而不是从代码中读取。

如果您确实需要这样做,请参阅this question和/或this website

但是,我建议gzipping存储图像的目录+ .html文件,并通过电子邮件发送该存档。

答案 2 :(得分:0)

$a