在R Markdown文件中包含HTML文件? [英] Include HTML files in R Markdown file?

查看:179
本文介绍了在R Markdown文件中包含HTML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速汇总



如何将HTML文件 置于 内的R Markdown档案内?



详情



我通过 choroplethr



由于链接演示,动画choropleths功能通过创建一组PNG图像,然后将其滚动到循环显示图像的HTML文件中,以显示动画。



但是现在我想将这些页面嵌入/合并到.Rmd文件中,这样我就可以得到一个包含这些动画choropleths的整体报告,与其他工作。



在我看来,应该有一个简单的方法来做相当于



链接:

  [请点击此处](http://this.is.where.you.will.go.html)



图片:

 ![可爱猫咪图片](http://because.that.is.what.we.need...another.cat.image .html)

图片路径正是我想要的:一个引用被炸毁把信息放在原处,而不仅仅是一个链接。我怎样才能做到这一点与完整的HTML文件,而不是一个图像?有什么办法吗?



通过示例解释



假设我的choropleth HTML文件位于我的本地路径中'./ animations / demographics.html',我有一个R Markdown文件,如:

  --- 
标题:'关注人口统计问题'
作者:Mike
日期:2016年4月9日
输出:
html_document:
number_sections:no
toc:yes
toc_depth:2
fontsize:12pt
---

#介绍

这是我想谈论的一些有趣的东西。但首先,让我们回顾一下我们之前看到的早期人口统计地图。
$ b $ [人口统计图]('./ animations / demographics.html')

我假设/假装 !! 是一个完全符合我想要的行为的先行词:允许我将该HTML文件嵌入其中的报告。

更新



两项更新。最近,我仍然无法完成任何工作,所以我将其全部推送到 GitHub存储库,以防万一有人愿意帮我解决问题。更多细节可以在repo的Readme文件中找到。



似乎能够将HTML嵌入到R Markdown文件中会非常有用,所以我一直试图对其进行排序它出。






(旧评论)



一些有用的建议,我尝试了,并在R Markdown文件中失败了以下内容:



闪亮的方法:

```{r showChoro1} 
shiny :: includeHTML(./ animations / demographics.html)
```

(我还在YAML部分中添加了 runtime:Shiny / b>

htmltools 方法:

 ```{showChoro1} 
htmltools :: includeHTML(./ animations / demographics.html)
```
pre>

(在这种情况下,我没有对YAML进行任何更改。)



在前一种情况下 Shiny ),它根本不起作用。事实上,包括HTML在内,似乎完全消除了文档的功能,因此运行时似乎永远不能完全实用。 (简而言之,虽然它似乎加载了所有内容,但加载spindel永远不会消失。)



在后一种情况下,没有其他任何事情搞砸了,但它是一个破碎的图像。奇怪的是,文件顶部有一个choropleth player功能区,可以工作,只是没有任何图像会弹出。






为了我自己的理智,我还提供了简单的链接,这些链接都正常工作。

  [This link] (./animations/demographics.html)在没有问题的情况下工作,只是它没有嵌入,就像我更喜欢的那样。 

所以这显然是嵌入的一个挑战。

解决方案

这是一个黑客(可能不雅)...想法是直接插入HTML编程在Rmd,然后渲染Rmd。



temp.Rmd文件:

  --- 
标题:简介
作者:chinsoon12
日期:2016年4月10日
输出:html_document
---

<<< insertHTML:[test.html]

等等等等
$ b $``{r,echo = FALSE}
htmltools :: includeHTML(test.html)
` ``

等等,等等

test.html文件:

 < html> 

< head>
< title>标题< / title>
< / head>

< body>

< p>这是一个R HTML文档。当您点击< b>针织HTML< / b>时,按钮将生成一个网页,其中包含文档内的任何嵌入式R代码块的内容和输出。您可以像这样嵌入R代码块:< / p>

< p> test test< / p>

< / body>
< / html>

使用HTML代码替换Rmd代码然后渲染的冗长代码(可能会缩短很多)

  library(stringi)
subHtmlRender< - function(mdfile,htmlfile){
#replace< ;< insertHTML:htmlfile with actual html code
#but without beginning space space
lines< - readLines(mdfile)
toSubcode< - paste0(<< insertHTML:[ ,htmlfile,])
location< - 其中(stri_detect_fixed(lines,toSubcode))
htmllines< - stri_trim(readLines(htmlfile))

#render html doc
newRmdfile < - tempfile(temp,getwd(),.Rmd)
newlines < - c(lines [1:(location-1)],
htmllines,
lines [min(location + 1,length(lines)):length(lines)])#当insertHTML是最后一行时,请小心.Rmd文件
write(newlines,newRmdfile)
rmarkdown :: render(newRmdfile,html_document)
shell(gsub( .Rmd,。html,basename(newRmdfile),fixed = T))
} #end subHtmlRender

subHtmlRender(temp.Rmd,test.html)






编辑:htmltools :: includeHTML也适用于示例我提供的文件。是因为你的特定html不喜欢UTF8编码吗?






编辑:将@MikeWilliamson评论带入反馈



我试过以下内容:复制和粘贴复制并粘贴


  1. 将github.com/lazarillo/persuasive_analytical_report/blob/master/SPM/animated_choropleth.htmlrel =noreferrer> animated_choropleth.html 添加到空白.Rmd中

  2. 删除对cloudfare.com,因为我有访问问题,而
    渲染(见下文)

  3. 编织HTML

  4. 放回这些云链接网页链接

  5. 将图形放入与呈现的HTML相同的文件夹中

  6. 打开HTML

我似乎找回了html,但我不确定结果是否符合您的期望



您是否也在pt 2中遇到同样的问题?您可能想要发布错误消息并要求修复:)。这是我的错误消息

  pandoc.exe:无法检索http://cdnjs.cloudflare.com/ajax/libs/ twitter-bootstrap / 3.1.1 / css / bootstrap.min.css 
FailedConnectionException2cdnjs.cloudflare.com80 False getAddrInfo:不存在(错误11001)
错误:pandoc文档转换失败并显示错误61


Quick Summary

How do I place HTML files in place within an R Markdown file?

Details

I have created some nice animated choropleth maps via choroplethr.

As the link demonstrates, the animated choropleths function via creating a set of PNG images, which are then rolled into an HTML file that cycles through the images, to show the animation. Works great, looks great.

But now I want to embed / incorporate these pages within the .Rmd file, so that I have a holistic report including these animated choropleths, along with other work.

It seems to me there should be an easy way to do an equivalent to

Links:

[please click here](http://this.is.where.you.will.go.html)

or

Images:

![cute cat image](http://because.that.is.what.we.need...another.cat.image.html)

The images path is precisely what I want: a reference that is "blown up" to put the information in place, instead of just as a link. How can I do this with a full HTML file instead of just an image? Is there any way?

Explanation via Example

Let's say my choropleth HTML file lives in my local path at './animations/demographics.html', and I have an R Markdown file like:

---
title: 'Looking at the demographics issue'
author: "Mike"
date: "April 9th, 2016"
output:
  html_document:
    number_sections: no
    toc: yes
    toc_depth: 2
fontsize: 12pt
---

# Introduction

Here is some interesting stuff that I want to talk about.  But first, let's review those earlier demographic maps we'd seen.

!![demographics map]('./animations/demographics.html')

where I have assumed / pretended that !! is the antecedent that will do precisely what I want: allow me to embed that HTML file in-line with the rest of the report.

Updates

Two updates. Most recently, I still could not get things to work, so I pushed it all up to a GitHub repository, in case anyone is willing to help me sort out the problem. Further details can be found at that repo's Readme file.

It seems that being able to embed HTML into an R Markdown file would be incredibly useful, so I keep trying to sort it out.


(Older comments)

As per some of the helpful suggestions, I tried and failed the following in the R Markdown file:

Shiny method:

```{r showChoro1}
shiny::includeHTML("./animations/demographics.html")
```

(I also added runtime:Shiny up in the YAML portion.)

htmltools method:

```{r showChoro1}
htmltools::includeHTML("./animations/demographics.html")
```

(In this case, I made no changes to the YAML.)

In the former case (Shiny), it did not work at all. In fact, including the HTML seemed to muck up the functionality of the document altogether, such that the runtime seemed perpetually not-fully-functional. (In short, while it appeared to load everything, the "loading" spindel never went away.)

In the latter case, nothing else got messed up, but it was a broken image. Strangely, there was a "choropleth player" ribbon at the top of the document which would work, it's just that none of the images would pop up.


For my own sanity, I also provided simple links, which worked fine.

[This link](./animations/demographics.html) worked without a problem, except that it is not embedded, as I would prefer.

So it is clearly a challenge with the embedding.

解决方案

Here is a hack (probably inelegant)...idea is to directly insert HTML programmatically in Rmd and then render Rmd.

temp.Rmd file:

---
title: "Introduction"
author: "chinsoon12"
date: "April 10, 2016"
output: html_document
---

<<insertHTML:[test.html]

etc, etc, etc

```{r, echo=FALSE}
htmltools::includeHTML("test.html")
```

etc, etc, etc

test.html file:

<html>

    <head>
    <title>Title</title>
    </head>

    <body>

        <p>This is an R HTML document. When you click the <b>Knit HTML</b> button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:</p>

        <p>test test</p>

    </body>
</html>

verbose code to replace Rmd code with HTML code and then render (can probably be shortened by a lot)

library(stringi)
subHtmlRender <- function(mdfile, htmlfile) {
    #replace <<insertHTML:htmlfile with actual html code
    #but without beginning white space
    lines <- readLines(mdfile)
    toSubcode <- paste0("<<insertHTML:[",htmlfile,"]")
    location <- which(stri_detect_fixed(lines, toSubcode) )
    htmllines <- stri_trim(readLines(htmlfile))

    #render html doc
    newRmdfile <- tempfile("temp", getwd(), ".Rmd")
    newlines <- c(lines[1:(location-1)],
                  htmllines,
                  lines[min(location+1, length(lines)):length(lines)])  #be careful when insertHTML being last line in .Rmd file
    write(newlines, newRmdfile)
    rmarkdown::render(newRmdfile, "html_document")
    shell(gsub(".Rmd",".html",basename(newRmdfile),fixed=T))
} #end subHtmlRender

subHtmlRender("temp.Rmd", "test.html")


EDIT: htmltools::includeHTML also works with the sample files that I provided. Is it because your particular html does not like UTF8-encoding?


EDIT: taking @MikeWilliamson comments into feedback

I tried the following

  1. copied and pasted animated_choropleth.html into a blank .Rmd
  2. remove references to cloudfare.com as I had access issues while rendering (see below)
  3. knit HTML
  4. put back those cloudfare weblinks
  5. put the graphs in the same folder as the rendered html
  6. open the HTML

I appear to get back the html but am not sure if the result is what you expect

Are you also facing the same issue in pt 2? You might want to post the error message and ask for fixes :). This was my error message

pandoc.exe: Failed to retrieve http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.1/css/bootstrap.min.css
FailedConnectionException2 "cdnjs.cloudflare.com" 80 False getAddrInfo: does not exist (error 11001)
Error: pandoc document conversion failed with error 61

这篇关于在R Markdown文件中包含HTML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆