Reduce file size of R Markdown HTML output

前端 未结 3 1131
滥情空心
滥情空心 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: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

提交回复
热议问题