如何使用 rmarkdown 创建独立的 html 报告?

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

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

是否可以获得带有相应图像的 "self-contained" .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")
  ) 
```

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

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

但是,我建议对存储图像的目录和您的 .html 文件进行 gzip 压缩,然后通过电子邮件发送该存档。

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

library(knitr)
library(rmarkdown)

render("caro.Rmd", html_document(pandoc_args = "--self-contained"))
browseURL("caro.html")
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