knitr/rmarkdown - reducing html file size
Following the suggestion of @daroczig to use the "dpi" knitr chunk option, I modified your code as follows (see below).
- You had set the dev chunk option equal to "svg", which produces very large vector graphics files, especially for images made up of many elements (points, lines, etc.)
- I set the dev chunk option back equal to "png", which is the default raster graphics format for HTML output. So you don't need to touch it at all. Keeping the dev chunk option equal to "png" dramatically reduces the HTML output file size.
- I set the dpi chunk option equal to 36 (72 is the default), to lower the image resolution, and decrease the HTML output file size further.
- I set the out.width and out.height chunk options equal to "600px", to increase the image dimensions.
- You can change the dpi, out.width, and out.height options, until you get the HTML output file size and the image dimension to what you want. There's a trade-off between output file size and image resolution.
After knitting the code, I got an HTML output file size equal to 653kB, even when plotting 5e4 data points.
---
title: "Change size of output HTML file by reducing resolution of plot image"
author: "My Name"
date: "September 7, 2015"
output: html_document
---
```{r}
# load ggplot2 silently
suppressWarnings(library(ggplot2))
# chunk option dev="svg" produces very large vector graphics files
knitr::opts_chunk$set(dev="svg")
# chunk option dev="png" is the default raster graphics format for HTML output
knitr::opts_chunk$set(dev="png")
```
```{r, dpi=36, out.width="600px", out.height="600px"}
# chunk option dpi=72 is the default resolution
set.seed(1)
mydf <- data.frame(x=rnorm(5e4),y=rnorm(5e4))
ggplot(mydf, aes(x,y)) + geom_point(alpha=0.6)
```
Reduce file size of R Markdown HTML output
The html_vignette
format is perfect if you want a smaller file size. As described in the function documentation:
A HTML vignette is a lightweight alternative to html_document suitable for inclusion in packages to be released to CRAN. It reduces the size of a basic vignette from 100k to around 10k.
For your example:
---
title: "Hello world!"
output: rmarkdown::html_vignette
---
Nothing else to say, really.
Results in an output of 6kB:
You can read more about the package in the online documentation here.
How can I reduce r markdown HTML file size when using external web images?
Thanks to J_F for pointing me to the magick
package in the comments.
The code below took my HTML from 1.2 MB to 940 KB with no resolution loss. Tweaking the scaling size while keeping the same out.width
in the chunk options lets me reduce the file size further if I'm willing to accept some image quality loss.
This works for now, but I'm going to keep this question open for a bit longer in case anyone has a solution that reduces the size further.
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, cache = TRUE)
options(scipen=999)
library(knitr)
library(magick)
library(tidyverse)
add_image <- function(filepath, size){
image_read(filepath) %>%
image_scale(str_extract(size, "[0-9]+"))
}
```
```{r, out.width = "600px", dpi = 300}
add_image("https://i.imgur.com/BacCbVa.png", 300)
```
Is it possible to reduce the size of a markdown html-report by including only specific parts of a package?
Plotly gives you the possibility to leverage threw different bundles.
This is done by partial_bundle()
.
The different sizes of the bundles can be found here:
- https://github.com/plotly/plotly.js/blob/master/dist/README.md#partial-bundles
For further information:
- https://plotly-r.com/performance.html
- https://www.rdocumentation.org/packages/plotly/versions/4.9.0/topics/partial_bundle
Knitr generating very large html files
My ad-hoc solution is to use the BiocStyle
style from Bioconductor. This reduces the html file size to 50 kb.
---
title: "This is a test"
author: "Mukul Pareek"
date: "June 7, 2017"
output:
BiocStyle::html_document
---
A more extreme solution is found here: How to render HTML from RMarkdown without javascript in output
It produces an html file smaller than 1 kb.
---
title: "This is a test"
author: "Mukul Pareek"
date: "June 7, 2017"
output:
html_document:
theme: null
highlight: null
mathjax: null
---
Related Topics
Double Clustered Standard Errors for Panel Data
How to Read Data with Different Separators
Harnessing .F List Names with Purrr::Pmap
Read Multiple Xlsx Files with Multiple Sheets into One R Data Frame
Filter a Vector of Strings Based on String Matching
R: What's the How to Overwrite a Function from a Package
Adding Prefix or Suffix to Most Data.Frame Variable Names in Piped R Workflow
How to Use a Graphic Imported with Grimport as Axis Tick Labels in Ggplot2 (Using Grid Functions)
The Perils of Aligning Plots in Ggplot
How to Handle Vectors Without Knowing the Type in Rcpp
Ternary Plot and Filled Contour
Knit One Markdown File to Two Output Files
How to Output Text to the R Console in Color
Display a Matrix, Including the Values, as a Heatmap
Convert Latitude and Longitude Coordinates to Country Name in R
Tidyverse - Prefered Way to Turn a Named Vector into a Data.Frame/Tibble