Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knitr generating very large html files

I am trying to use Knitr to write simple text-only html pages, but the file sizes are extremely large - 700KB for a 1 line RMarkdown. I am on R 3.4.0, and R Studio 1.0.143

For example, the following RMarkdown file generates a 695 kb file. When I look at the source, that is because there is a huge amount of base 64 code under the script tag. Is there anything that can be done to make the file size more compact, say a couple of KBs that I would expect it to be

(eg script src="data:application/x-javascript;base64,LyohIGpRdW...and this goes on and on forever)

---
title: "This is  a test"
author: "Mukul Pareek"
date: "June 7, 2017"
output: html_document
---


## This is a test file which when knit to html is 695KB
like image 512
Topchi Avatar asked Jun 07 '17 18:06

Topchi


People also ask

How do I convert RMD to html?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

What is knitr used for?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.

What is a knitr document?

knitr is an R package that integrates computing and reporting. By incorporating code into text documents, the analysis, results and discussion are all in one place.

How do you add a table of contents to a RMD?

You can embed an R code chunk like this: ```{r} summary(cars) ``` You can also embed plots, for example: ```{r, echo=FALSE} plot(cars) ``` ### Header 3 Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.


1 Answers

The key to reduce the HTML file size is to set theme: null, which means to get rid of the giant Twitter Bootstrap styles. Below are a few examples:

---
title: "This is  a test"
author: "Mukul Pareek"
date: "June 7, 2017"
output:
  html_document:
    theme: null
  html_vignette: default
  prettydoc::html_pretty: default
---


## This is a test file which when knit to html is 695KB

html_document(theme = NULL) returns a 44kb file; html_vignette returns 6.1kb; prettydoc::html_pretty returns 63.7kb (you need to install the prettydoc package). If you want a "flashy" style, I think prettydoc has achieved a great balance between styles and file sizes; otherwise you have to bear with those "vanilla" styles.

like image 53
Yihui Xie Avatar answered Sep 30 '22 15:09

Yihui Xie