knitr (R) - how not to embed images in the HTML file?

前端 未结 4 752
醉梦人生
醉梦人生 2020-12-05 18:13

This is probably very easy but I can\'t seem to find it in docs. I would like to not embed the generated images in the HTML file itself.

So basically I want knit2htm

相关标签:
4条回答
  • 2020-12-05 18:44

    Its not knitr that does this, knitr just produces a modified markdown file after running the R chunks. So you need to look at the help for the markdown package to figure out...

    Its the base64_images option. Coffee hasn't kicked in yet, so I haven't exactly sussed out how to set/reset individual markdown options, but clearing them all out works for me:

     > knit2html("foo.Rmd",options="")
    

    producing

     <p><img src="figure/unnamed-chunk-1.png" alt="plot of chunk unnamed-chunk-1"> </p>
    

    in foo.html.

    If clearing all those options breaks other stuff, then read up on markdownHTMLOptions.

    0 讨论(0)
  • 2020-12-05 18:48

    Here is a simple way to have figures in a separate html file, which will reduce its size significantly.

    Add this chunk in the beginning of the *.rmd file:

    ```{r global_options, include=FALSE}
    #suppress the warnings and other messages from showing in the knitted file.
    knitr::opts_chunk$set(fig.width=8, fig.height=6, fig.path='Figs/',
                          echo=TRUE, warning=FALSE, message=FALSE)
    ```
    

    Option 'fig.path' tells R to save pictures into 'Figs' folder. The rest of options is not required for the task.

    Click this button:

    Make sure the check box is not checked:

    0 讨论(0)
  • 2020-12-05 18:56

    You can just add self_contained: no to the output options in the .Rmd header. For example:

    ---
    title: "Data visualisation with ggplot"
    output:
      html_document:
        self_contained: no
        toc: yes
        toc_float: yes
    ---
    
    0 讨论(0)
  • 2020-12-05 18:58

    If you look at the knit2html help page, you will see that :

    This is a convenience function to knit the input markdown source and
    call ‘markdownToHTML()’ in the ‘markdown’ package to convert the
    result to HTML.
    

    Then you look at the markdownToHTML help page and read that there is the following argument :

     options: options that are passed to the renderer.  see
               ‘markdownHTMLOptions’.
    

    So you look at the markdownHTMLOptions (still not lost ?) and see the following option :

     ‘'base64_images'’ Any local images linked with the ‘'<img>'’ tag
          to the output HTML will automatically be converted to base64
          and included along with output.
    

    With the following command, you should see the default options on your system :

    R> markdownHTMLOptions(default=TRUE)
    [1] "use_xhtml"      "smartypants"    "base64_images"  "mathjax"       
    [5] "highlight_code"
    

    So may be you can try to knit your markdown file with :

    knit2html("knit.Rmd", options=c("use_xhtml","smartypants","mathjax","highlight_code"))
    

    Not tested, though...

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