Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 knit2html() to produce a HTML file with seperate image files (which are then linked to / shown in the HTML). The basic behaviour is that the script embeds the images as a base64 string. The problem with this is that in IE, large images won't show up (i.e. appear to be missing). Any idea how I can seperate the images from the HTML output?

My example .Rmd file ('knit.Rmd'):

```{r} plot(3) ``` 

And my .R file to generate the HTML from this:

library(knitr)  knit2html('knit.Rmd') 

This example generates a HTML with the plot as an embedded base64 string.

like image 395
Bart Avatar asked Feb 14 '13 08:02

Bart


People also ask

How do I convert RMarkdown 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.

How do I insert a JPEG into R markdown?

To insert an image, you can also use an R code chunk. --- title: "R Markdown Tips and Tricks" output: html_document --- You can also insert an image using R code: ```{r} knitr::include_graphics("img/rmarkdown_hex. png") ``` The image looks smaller by default though.

How do I knit RStudio to HTML?

To knit in RStudio , click the Knit pull down button. You want to use the Knit HTML option for this lesson. When you click the Knit HTML button, a window will open in your console titled R Markdown. This pane shows the knitting progress.

How do you use the knitr in RStudio?

If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.


1 Answers

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...

like image 187
juba Avatar answered Sep 23 '22 21:09

juba