2013-02-14 53 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 
---