Rmarkdown : embed html file to ioslides - html

I produce interactive plots with plotly, and save them as html pages. I'm new to ioslides and Rmarkdown, so I searched through the internet, and found no solution. I want to include a plotly graph with .html extension to my slide. So I tried:
---
title: "Advanced Physics Project"
author: "mcandar"
output:
ioslides_presentation:
widescreen: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Introduction
![my graph](myfile.html)
and it does not work. What I want is a slide with my html graph embedded inside and its interactive features should work properly. Is it possible?

Option 1: Use an iframe:
---
title: "Advanced Physics Project"
author: "mcandar"
output:
ioslides_presentation:
widescreen: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
<iframe src="myfile.html"></iframe>
Option 2: includeHTML
```{r}
shiny::includeHTML("myfile.html")
```

Related

Add HTML and javascript code to flexdashboard in R

I have the following code which creates a flexdashboard:
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
And I want to insert some HTML and javascript code. I tried this
Column
-----------------------------------------------------------------------
### Block 1
```{r}
<p>"This is a paragraph"</p>
<script>
alert("This is an alert")
</script>
```
But it doesn't work. Please, could you help me with this question? Thank you.
You can directly type the HTML code, without chunk. You can also use the tags function of the 'htmltools' package in a chunk (or the Shiny UI functions). For JavaScript, use a js chunk.
---
title: "TEST"
output:
flexdashboard::flex_dashboard:
orientation: rows
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r packages, include=FALSE}
library(flexdashboard)
library(htmltools)
```
Page
====================================
Row
-----------------------------
### HTML and JavaScript
<button id="btn">Click me</button>
```{js, echo=FALSE}
$("#btn").on("click", function() {
alert("You clicked the button!")
})
```
### HTML using 'htmltools'
```{r, echo=FALSE}
tags$button("Another button")
```

Rmarkdown 'saving' an exact place of the html page

I've created two Rmarkdown pages that are linked together. I'd like to know if there is a way to code that the links direct to a specific spot on each page.
---
title: "Page 1"
author: "--"
date: "22 9 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Header
example text.
#### example plot 1.
```{r cars}
plot(cars)
```
more example text
#### example plot 2.
```{r pressure, echo=FALSE}
plot(pressure)
```
link to [Page 2](./Page-2.html)
And Page 2:
---
title: "Page 2"
author: "--"
date: "22 9 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Header
example text.
#### example plot 1.
```{r cars}
plot(cars)
```
more example text
#### example plot 2.
```{r pressure, echo=FALSE}
plot(pressure)
```
[Page 1](./Page-1.html)
For example, I'd like the page 2 link to redirect straight to the second graph. Moreso, when clicking back to page 1 - is there a way for it to save the spot I was currently looking at?
Thanks.
This feature is not provided for HTML output of rmarkdown/knitr. However, you can easily implement this using javaScript (jquery):
The idea is to include a small script that saves the last scroll position in a variable (I've called it ScrPos below) in local memory and restores the scroll position when the page is (re)loaded.
header.html
<script type="text/javascript">
// check if ScrPos is available. If so, scroll to ScrPos
$(function() {
if (localStorage.ScrPos) {
$(window).scrollTop(localStorage.ScrPos);
}
});
// save last scroll position to ScrPos before page unload
$(window).on("beforeunload", function() {
var ScrPos = $(window).scrollTop();
localStorage.setItem("ScrPos", ScrPos);
});
</script>
You may include header.html in the YAML header (of both) .rmd files like this:
output:
html_document:
include:
in_header: header.html
Note that header.html needs to be in the same directory as the .Rmd.
for the linking to the second graph part it can be easily achieved by using the div id that's used for the second graph, either by explicitly assigning it or by using transforming the spaced section title to a dash separated one `example plot 2.` => id = `example-plot-2.` i.e:
New code for page 2
#### example plot 2.{#second_graph}
```{r pressure, echo=FALSE}
plot(pressure)
```
[Page 1](./Page-1.html)
and then in page one simply add the id to the link i.e:
link to [pressure graph](./Page-2.html#second_graph)

Links in RMarkdown Datatable

I'm trying to create active links in an RMarkdown document. In the code below, the basic kable table is perfect, but the datatable version turns the links into text
---
title: "Example"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(DT)
example.df <- data.frame("link"=c("[Tokyo](https://en.wikipedia.org/wiki/Tokyo)","[Paris](https://en.wikipedia.org/wiki/Paris)"), "country"=c("Japan","France"))
kable(example.df)
datatable(example.df)
Whoops, here's the answer: format the links as raw HTML not as RMarkdown syntax http://blogs.oregonstate.edu/cgrb/2019/08/06/r-tips-a-table-makeover-with-dt/
There are 2 changes needed to it to work with datatable.
Format the links as raw HTML, not Markdown
Include relevant escape arguments as to NOT escape the link column. For simplicity in this example, we use escape=FALSE
Here is a full reprex answer.
---
title: "Example"
output: html_document
---
```{r setup, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(DT)
example.df.kable <- data.frame("link"=c("[Tokyo](https://en.wikipedia.org/wiki/Tokyo)","[Paris](https://en.wikipedia.org/wiki/Paris)"), "country"=c("Japan","France"))
example.df.datatable <- data.frame("link"=c(
'Tokyo',
'Paris'),
"country"=c("Japan","France"))
kable(example.df.kable)
datatable(example.df.kable) # This does NOT show the links
datatable(example.df.datatable) # This does NOT show the links
datatable(example.df.datatable, escape=FALSE) # This does show the links

Export mermaid chart from DiagrammeR

I'm attempting to export a Gantt chart from mermaid to a file through R. I'd be happy with any file format, but SVG or PNG would be preferable. I'm trying to automate this, so simply pressing export through the GUI is not an option.
Here's my code:
library(DiagrammeR)
graph <- mermaid("
gantt
dateFormat HH:mm:ss.SSS
title Sample Test Gantt
section A
thing1 : 15:58:51.556, 16:05:23.494
section B
thing2 : 16:02:00.391, 16:20:46.533
section C
thing3 : 16:18:57.352, 16:23:10.700
thing4 : 16:24:11.705, 16:30:30.432
")
graph
And the graph it generates:
This is a reported issues with the DiagrammeR package, so you may want to keep an eye on this page for future updates: https://github.com/rich-iannone/DiagrammeR/issues/66
There are two ways this could be done as a workaround:
Using Webshot
An alternative way of saving the file is to use the webshot package. This uses the external dependency phantomjs to convert the HTML widget to an image. It can be setup as follows:
install.packages("webshot")
webshot::install_phantomjs()
Using your above example:
library(DiagrammeR)
library(magrittr)
gannt %>%
htmltools::html_print() %>%
webshot::webshot(file = "gannt.pdf")
This will save the plot as a PDF, but you can create images by changing the filename i.e. gannt.png.
Using the plotly package
The plotly package has a useful function for exporting HTML widgets:
plotly::export(gannt, file = "mermaid.png")
From what I know about mermaid it is not possible yet to export to svg or other formats. But it is possible to dump many mermaid objects to an HTML via Rmd:
---
title: "Untitled"
author: "Me"
date: "August 1, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document.
```{r echo=FALSE, warning=FALSE, message=FALSE}
library(DiagrammeR)
graph <- mermaid("
gantt
dateFormat HH:mm:ss.SSS
title Sample Test Gantt
section A
thing1 : 15:58:51.556, 16:05:23.494
section B
thing2 : 16:02:00.391, 16:20:46.533
section C
thing3 : 16:18:57.352, 16:23:10.700
thing4 : 16:24:11.705, 16:30:30.432
")
graph
graph
graph
```
It produces an HTML file with all the graphs in it. Not an optimal solution, but better than trying to produce lots of charts manually.
Marked mysteRious' answer as correct, as it really helped. For anyone in the future that runs into the same issue, here's the full solution I used:
Export.Rmd
---
title: "TestingExport"
author: "me"
date: "August 2, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r echo=FALSE, warning=FALSE, message=FALSE}
library(DiagrammeR)
graph <- mermaid("
gantt
dateFormat HH:mm:ss.SSS
title Sample Test Gantt
section A
thing1 : 15:58:51.556, 16:05:23.494
section B
thing2 : 16:02:00.391, 16:20:46.533
section C
thing3 : 16:18:57.352, 16:23:10.700
thing4 : 16:24:11.705, 16:30:30.432
")
graph
```
Then use the following command to turn this into HTML:
Rscript -e "rmarkdown::render('...\\Export.Rmd')"

knitr Markdown LateX like table within HTML document

I would like to produce "LaTeX-like" table within an HTM document using knitr markdown (.Rmd) through:
knitr::knit2html(input="D:/...Rmd", output="D:/...report.html")
Here is an example. However, if I decided to produce a report, the LaTeX table would be incorrect:
library(xtable)
xtabl <- xtable(head(CO2))
print(xtabl, type="latex", include.rownames=FALSE)
The above gives:
As suggested here is the result. It was NOT a "LaTeX-like" table!
xtabl <- xtable(head(CO2))
print.xtable(xtabl, type="html", include.rownames=FALSE)
EDIT:
What I mean by "LaTeX-like" table is this:
The R Markdown cheat sheet provides a visual comparison of libraries kable, xtable and stargazer. Stargazer could be what you are looking for.
Also have a look into the htmlTable package.
Further customizations could be made with a custom CSS file.
Here's an example of a basic table with htmlTable:
---
title: "Untitled"
author: "Author"
date: "2/5/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(htmlTable)
```
```{r, results="asis"}
tab = cbind.data.frame(
sapply(iris[1:5 , sapply(iris, is.numeric)], function(x) sprintf("%1.1f", x)),
Species=iris$Species[1:5]
)
htmlTable(tab, rnames=FALSE, align="rrrrr", align.header="rrrrr",
css.cell = c(rep("padding-left: 5em", 4), "padding-left: 2em"))
```
I have used knitr::kable for producing the desired tables.
mydata <- data.frame(SrNo=c(1,2,3,4), websites=c("stackoverflow", "twitter", "facebook", "google"))
knitr::kable(mydata)
kable function accepts a format argument with possible values latex, html, etc. see the documentation for details
Complete Markdown file
---
title: "kable"
author: "Imran Ali"
date: "February 6, 2017"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo=FALSE}
mydata <- data.frame(SrNo=c(1,2,3,4), websites=c("stackoverflow", "twitter", "facebook", "google"))
knitr::kable(mydata)
```