Reduce file size of R Markdown HTML output

前端 未结 3 1130
滥情空心
滥情空心 2020-12-11 02:30

If I create a very basic R Markdown file with no images or code and knit this HTML, I end up with an outputted file size that is more than 700kb in size. Is there any way to

相关标签:
3条回答
  • 2020-12-11 03:13

    The simplest, most direct method to prevent the unwanted insertion of the bootstrap libraries into the preamble of the HTML document is to add the additional markdown flag "theme: null".

    output:
      html_document:
         theme: null
    

    This is more desirable than self_contained: false because it does not prevent insertion of images or other components need to keep the portable document.

    In my opinion, it is more desirable than changing to html_vignette because it does not absorb the other changes imposed by that processor.

    Please remember that IF your document uses a template, the theme argument is ignored and you need to specify theme=NULL in the rmarkdown::render function.

    0 讨论(0)
  • 2020-12-11 03:21

    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.

    0 讨论(0)
  • 2020-12-11 03:27

    The reason for the big file size is that knit creates self-contained files by default and therefore includes javascript dependencies (bootstrap, highlight, jquery, navigation) as base64 encoded string. See: http://rmarkdown.rstudio.com/html_document_format.html#document_dependencies

    In your simple case the javascript capabilities are not required therefore you could do the following:

    ---
    title: "Hello world!"
    output:
      html_document:
        self_contained: false
        lib_dir: libs
    ---
    
    Nothing else to say, really.
    

    This will create a html file of size ~2.7kB and a separate libs folder with the javascript files. However the libs folder is nearly 4MB in size. And although you don't necessarily need the javascript libraries the html file still tries to load them.

    If you are interested in a truly minimal version you can have a look at the html_fragment output option (http://rmarkdown.rstudio.com/html_fragment_format.html):

    ---
    title: "Hello world!"
    output:
      html_fragment: default
    ---
    
    Nothing else to say, really.
    

    This will however not create a full html page but rather html content that can be included into another website. The test.html file is just 36 bytes. Still browsers will be able to display it.

    As a last resort you can create a custom html template for pandoc: http://rmarkdown.rstudio.com/html_document_format.html#custom_templates

    0 讨论(0)
提交回复
热议问题