微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

knitr(R) – 如何在HTML文件中嵌入图像?

这可能很简单,但我似乎无法在文档中找到它.我不想将生成的图像嵌入到HTML文件本身.

所以基本上我想要knit2html()生成一个HTML文件与单独的图像文件(然后链接到/显示在HTML中).基本的行为是脚本将图像作为base64字符串嵌入.这样做的问题是在IE中,大图像将不会显示(即看起来是丢失的).任何想法如何从HTML输出中分离图像?

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

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

和我的.R文件从此生成HTML:

library(knitr)

knit2html('knit.Rmd')

此示例生成一个HTML作为嵌入的base64字符串.

解决方法

如果您查看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"

所以可能你可以尝试编织你的markdown文件

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

没有测试,虽然…

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐