Mimic R CMD build HTML vignette: Embed external images as self contained - html

I would like to build an HTML in the same way that a package build makes Rmd files into self contained HTML files. It even takes an image referenced from an external file (say a png file) and converts it to something like the following:
<div style="width:367.5px;margin:auto;">
<p><img
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhkAAAGpCAYAAAAgOC
RjAAAABmJLR0QA/wD/AP+gvaeTAAAgAElE
ABOUT_1_TRILLION_SEEMINGLY_RANDOM_CHARACTERS_xdfhf" width="350"
height="250"></p>
</div>
I know this to be the case because I use an external file ![](imgs/tm_imgs/img2.png) and it renders in the doc directory as the code above. This tells me that buildVignettes used in R CMD build is converting/encode these external files to the base64. I can almost mimic the behavior of the R CMD build vignettes except the external images using:
MWE
dir.create("delete_me")
setwd("delete_me")
dir.create("imgs")
dir.create("output")
png("imgs/fake_external_file.png")
plot(1:10)
dev.off()
x <- readLines(n=13)
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(cache=FALSE, tidy=FALSE, warning=FALSE)
opts_knit$set(upload.fun = image_uri, self.contained=TRUE)
```
```{r}
plot(cars);lines(lowess(cars))
```
![](imgs/fake_external_file.png)
The End
cat(paste(x, collapse="\n"), file="test.Rmd")
knitr::knit2html("test.Rmd", output = "output/test.html",
options=c("base64_images"))
But then I open the HTML file and only see (missing second external image):
Because the html source is still:
<p><img src="imgs/fake_external_file.png" alt=""></p>
rather than the base64 encoded.
Question
How can I make knitr and/or markdown packages behave in the same manner as R CMD build for making external images self contained with the base64 encoding? If someone has a different angle than the suggested knitr and/or markdown and an external package is necessary it would need to be CRAN.

When your output contains external dependencies generated from R code, you are strongly recommended not to write the output file to a different directory, because the structure of relative paths may confuse several tools in the chain, including knitr, markdown, Pandoc, LaTeX, and so on. This has been documented in the Note section in ?knitr::knit. A short answer to this question is
setwd('output')
knit2html('../test.Rmd')
Note the output argument is passed to knit() (as documented in knitr::knit2html), so foo.html is actually not appropriate (foo.md is). Yes, I know this is confusing, and naturally users expect the output argument for knit2html() to be a HTML file.
A slightly longer answer and explanation: in your case, by imgs/fake_external_file.png, you mean it is relative to output/test.html, i.e. it is output/imgs/fake_external_file.png, but actually this image is under your working directory ./ instead of ./output/. The reason that the plot from the code chunk works is that the plot is written to figure/foo.png relative to the current working directory. To avoid the pain of thinking relative directories (relative to which?), just set the working directory to the directory in which you want to generate output, and use a single relative path for the input file. You rarely need to set the output argument unless you want to change the basename of the output file (e.g. knit('foo.Rmd', output = 'bar.md')).

Here's one approach I got by looking at image_uri, but I'm hoping for a better approach:
dir.create("delete_me")
setwd("delete_me")
dir.create("imgs")
dir.create("output")
png("imgs/fake_external_file.png")
plot(1:10)
dev.off()
x <- readLines(n=19)
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(cache=FALSE, tidy=FALSE, warning=FALSE)
opts_knit$set(upload.fun = image_uri, self.contained=TRUE)
uri_embed <- function(path) {
uri <- knitr::image_uri(path)
cat(paste0("<img src=\"", uri, "\" />"))
}
```
```{r}
plot(cars);lines(lowess(cars))
```
```{r, echo=FALSE, results='asis'}
uri_embed("imgs/fake_external_file.png")
```
The End
cat(paste(x, collapse="\n"), file="test.Rmd")
knitr::knit2html("test.Rmd", output = "output/test.html",
options=c("base64_images"))

Related

How can I change the graphics device for HTML output in R markdown without saving individual plots?

I am trying to compile various plots in an R markdown file. I would prefer that the HTML output produced images using scalable vector graphics instead of the default PNG images. Currently, I am using the following method to achieve this:
---
title: "SVG Plot Test"
output:
html_document:
df_print: paged
---
```{r setup-chunk}
knitr::opts_chunk$set(dev = "svg")
```
```{r test_plot}
library(ggplot2)
ggplot(airquality, aes(x = Temp, y = Wind)) +
geom_point()
```
However, when I knit these chunks to HTML, an additional folder is created and the plot is saved within that folder as well. With the large number of plots I am creating, I'd prefer not to save these copies as well, since they are already present within the resulting HTML document.
How can I prevent these additional files from being created while still getting svg output in the HTML file?

How to save html output in Rmarkdown

This is the first time I am using rmarkdown to knit a document into an html output. I see the output as an html document, no problem. However the output is linked to my personal working directory (example:file:///C:/Users/e337384/table_formatting.html).
---
title: "Inter-Rater Agreement (Long Beach)"
output:
html_document: default
word_document: default
date: "2/6/2020"
---
```{r,echo=FALSE, message=FALSE}
library(knitr)
require(kableExtra)
library(tidyverse)
options(knitr.table.format = "html")
```
I would like to be able to share the html document with my team, but since the output is linked to my drive only I can see it. How do I save this output in a way that allows me to share this html output?!
Recently, the export mechanism for files has changed slightly.
Now you have to check the corresponding check-box of the file you wish to export and then select More -> Export, as shown in this picture:

How to embbed images in jekyll-built blog with 'brocks' package

Following the steps of Brendan on this post https://brendanrocks.com/blogging-with-rmarkdown-knitr-jekyll/ I was able to successfully build my blog using knitr-jekyll and R markdown. However when it comes to embedding images on the .Rmd I am having a lot of trouble. The post output does not show any images embedded (just the plots produced by coding).
I tried using knitr::include_graphics() and also pure HTML tags (both ways directly in the .Rmd file) but got only empty spaces.
Thinking about the possibility of the problem being with paths I tried to move images to the figure/source folder but got nothing. Then I tried moving to a new folder called _data and adjusting the path on include_graphics() but got nothing again.
OS: Ubuntu 16.04
Current code with include_graphics():
```{r, out.width="900px", echo=FALSE}
knitr::include_graphics("path_root_site/_data/engrenagem.png")
```
```{r, out.width="900px", echo=FALSE}
knitr::include_graphics("path_root_site/_data/engrenagem2.png")
```
current code with pure HTML (another image):
<img src="path_root_site/_data/canvas.png" alt="gráfico sage" width=1000 height=500></img>

Include HTML files in R Markdown file?

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
copied and pasted animated_choropleth.html into a blank .Rmd
remove references to cloudfare.com as I had access issues while
rendering (see below)
knit HTML
put back those cloudfare weblinks
put the graphs in the same folder as the rendered html
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
Did you try the includes: option in your YAML header?
https://rmarkdown.rstudio.com/html_document_format.html#includes
But maybe you'll have the same problem I have: I'd like to include the HTML file in a specific section in my RMarkdown document, not in the header or before/after body.
can try put this line in the Rmarkdown and then knit.
(YAML header "output: html_document"; if "runtime: shiny" somehow it does not work)

Using knitr (from .Rhtml to html): how to embed a link in an R Figure?

I am using knit to convert my .Rhtml file to an .html file.
I am calling the output of a chunk called Q1:
<!--begin.rcode Q1,echo=FALSE,fig.show="all",fig.align="center",warning=FALSE
end.rcode-->
Here comes the chunk, it is basically a ggplot2 figure in a 2x2 layout.
library(ggplot2)
myplot = list()
for (i in 1:4){
x = 1:100
y = sample(100,100)
data = data.frame(x=x,y=y)
myplot[[i]] = ggplot(data,aes(x=x,y=y))+geom_point()+labs(title="bla")}
do.call(grid.arrange,c(myplot,list(nrow=2,ncol =2)))
Now, when looking at the resulting html file, I would like to incorporate the following feature:
I would like to have a link (e.g. to a database) when clicking on the title of each plot.
Is this somehow possible?
Thx
This doesn't completely answer your question, but it might get you or someone else started on a full answer.
Paul Murrel's gridSVG package (see also this useful pdf doc) allows one to add hyperlinks to grid-based SVG graphics. (In theory it should thus work with ggplot2; in practice I've just got it working with lattice). The current issue of the R Journal includes a couple of articles ("What's in a name?" and "Debugging grid graphics." -- Warning: pdfs) that might help you to best design dynamic searches for name of the grob to which you'd like to add a link (as in my second line of code).
library(gridSVG)
library(lattice)
xyplot(mpg~wt, data=mtcars, main = "Link to R-project home")
mainGrobName <- grep("main", grid.ls()[[1]], value=TRUE)
grid.hyperlink(mainGrobName, "http://www.r-project.org")
gridToSVG("HyperlinkExample.svg")