简体   繁体   中英

knitr: how to stop base64 image embedding in R vignette

I have a simple test Rmd vignette in my R package:

---
title: "Vignette Test"
author: Snowy Seal
date: "`r format(Sys.Date(), '%B %d, %Y')`"
output:
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 3
vignette: >
  %\VignetteIndexEntry{Vignette Test}
  %\VignetteEngine{knitr::rmarkdown}
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(out.extra='', fig.pos='H', collapse=TRUE, comment=NA)
``` x

A simple plot:

```{r simpleplot, fig.width=7, fig.height=6, fig.align='center'}
plot(1:10)
``` x

(Sorry, the spurious 'x' chars are to let me embed the extra backtick blocks!)

Normally the resources of the generated HTML file (css/images) are embedded to make single html file. Images are embedded using data uri mechanism which uses base64 encoding in the image. For a large number of plots this makes the HTML file huge and my final package too big for CRAN.

According to an answer to a previous question about this stackoverflow/questions/14870589 the way to stop base64 data uri image embedding in HTML output files is to specify the option self_contained: no in the YAML header of your Rmd file (next to the toc: true option).

This does indeed have an effect - the img tags now have src="path/to/image.png" links, BUT the installed R package does not contain these external resource files.

The R package build and install with vignette processing is complex and I can't understand what I need to do to get these external resources installed into the R package doc/ directory.

Here's one way to do it. This is somewhat error-prone, and might not help enough with the size of your package: base64 expands things, but only by about 33%, so I'd think about other ways to reduce your size instead.

The idea is to process your vignette in the vignettes directory before building your package. If your vignette is called test.Rmd and you have in your YAML header

output:
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 3
    self_contained: false

then you'll find processing it by rmarkdown::render("test.Rmd") creates a directory called test_files , which will contain your plots. To get this into your final package, create another file in the vignettes directory called .install_extras , and put the line

test_files

in it. (This is a regexp pattern which will match the path of any file in that directory. If it catches too much, make it more specific.)

I don't advise doing this, however. It is quite error prone, because if you just click on "knit" in RStudio, the plots in the test_files directory won't be updated. It will be really easy to edit the vignette and forget to update the plots. You're better off finding some other way to reduce the size of your package, or publishing it somewhere else besides CRAN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM