2013-02-14 67 views
19

這可能很容易,但我似乎無法在文檔中找到它。我想不將生成的圖像嵌入HTML文件本身。knitr(R) - 如何不在HTML文件中嵌入圖像?

所以基本上我希望knit2html()生成一個帶有單獨圖像文件(然後鏈接到/顯示在HTML中)的HTML文件。基本行爲是腳本將圖像嵌入爲base64字符串。這樣做的問題是,在IE中,大圖像不會顯示出來(即似乎丟失)。任何想法如何我可以從HTML輸出分離圖像?

我的例子.Rmd文件( 'knit.Rmd'):

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

而且我.R文件,以從這個HTML:

library(knitr) 

knit2html('knit.Rmd') 

這個例子產生與一個HTML繪製爲嵌入的base64字符串。

+0

你能不能給我們的,你在做什麼很短的降價例子,列出你調用R的功能... – Spacedman 2013-02-14 08:30:57

+0

我添加了一個小例子。 – Bart 2013-02-14 08:51:03

回答

16

如果你看一下knit2html幫助頁面,你會發現:

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

然後你看看markdownToHTML幫助頁面和閱讀,有以下幾種說法:

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

所以你看看markdownHTMLOptions(仍然沒有丟失?)並看到以下選項:

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

用下面的命令,你會看到你的系統上的默認選項:

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

那麼可能是你可以嘗試編織你的降價文件,:

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

沒有測試過,雖然..

+0

謝謝,作品像一個魅力! – Bart 2013-02-14 10:00:19

10

它不是knitr這樣做,knitr只是在運行R塊後生成修改的降價文件。因此,您需要查看markdown包的幫助以找出...

它的base64_images選項。咖啡還沒有在尚未踢,所以我還沒有確切sussed如何設置/復位個別降價的選擇,但他們清理出來的所有作品對我來說:

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

生產

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

foo.html

如果清除所有這些選項打破其他的東西,然後閱讀markdownHTMLOptions

+0

首先,將.Rmd文件中的R塊渲染爲修改後的knitr .md文件。然後呈現爲.html,例如'渲染( 'foo.Rmd')'。 R解釋器顯示通用knitr文件名。 https://www.r-bloggers.com/r-knitr-markdown-html/ – noobninja 2016-10-29 16:16:31

4

下面是一個簡單的方法在一個單獨的html文件中有數字,這將顯着減小其大小。

將該塊添加到*的開頭。RMD的文件:

```{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) 
``` 

選項 'fig.path' 告訴R鍵的圖片保存到 '圖' 的文件夾。任務的其餘選項不是必需的。

點擊此按鈕:

Click this button

確保未選中該複選框:

Make sure the check box is not checked

4

您可以再補充self_contained: no在.Rmd頭輸出選項。例如:

--- 
title: "Data visualisation with ggplot" 
output: 
    html_document: 
    self_contained: no 
    toc: yes 
    toc_float: yes 
---