Export mermaid chart from DiagrammeR - html

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')"

Related

Displaying regression summary table with stargazer()

I wanted to add a table of the regression summary to my html final document with the stargazer() function however I do not understand why it keeps displaying a text format table . Can someone help me please ?
stargazer::stargazer(reg1 , title = "Arrests per capita VS Video games revenue per capita" , type = "latex",keep.stat = c("n","rsq","adj.rsq","f"))
Sounds like you produce your code in an html format, like markdown. I suggest using gtsummary like that:
---
title: "Regression"
author: "Author"
output: html_document
---
## Model
```{r model}
model <- lm(mpg ~ cyl + disp + wt, data=mtcars)
```
## Summary
```{r summary, warning=FALSE, message=FALSE}
library(gtsummary)
tbl_regression(model)
```

Displaying html file using htmltools::tags$iframe in shiny is not working with renderUI()

This is my first question in StackOverflow. Please forgive me if the question is not represented in the proper format.
I have a saved html widget, generated using flowmapblue.R that I want to display in a Markdown Shiny Document.
I am using htmltools::tags$iframe to include the HTML file in the Shiny app. The file is kept under the www directory. For your kind reference, I am sharing the HTML file in the following LINK.
The code that is working and giving desired result is:
---
title: "Flow Map"
author: "xyz"
date: "8/14/2020"
output: html_document
runtime: shiny
---
```{r flowmap, echo=FALSE, message=FALSE, warning=FALSE}
titlePanel("Mobility Flow Map")
mainPanel (htmltools::tags$iframe(src ="June_Spain.html", seamless="seamless", height=600, width="100%"))
```
I am getting this result Result without using renderUI.
But actually my Markdown Shiny document will be responsive where the user can select zones and desired months. Based on those names the relevant HTML file will be selected. That's why I need to use the next following code snippet:
---
title: "Flow Map"
author: "xyz"
date: "8/14/2020"
output: html_document
runtime: shiny
---
```{r flowmap, echo=FALSE, message=FALSE, warning=FALSE}
titlePanel("Mobility Flow Map")
mainPanel(
htmlOutput("display_map")
)
output$display_map <- renderUI({
my_test <- htmltools::tags$iframe(src="June_Spain.html", seamless="seamless", height=600, width="100%")
my_test
})
```
In this case the HTML File is not Found as shown in Result using renderUI.
I checked few similar problems with renderUI() and htmlOutput() but I couldn't make out where it is going wrong. I desperately need your help in this regard. Thanks in advance.
Strange. As a workaround you can encode the HTML to a base64 string:
b64 <- base64enc::dataURI(file = "www/June_Spain.html", mime = "text/html")
output$display_map <- renderUI({
htmltools::tags$iframe(src=b64, height=600, width="100%")
})

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

Looking for a simple way to include profvis output in a Markdown file in R

The title pretty much says it all. I want to profile a function in R (with the profvis package) and display the output in an R Markdown file, without manually creating a screenshot and ideally with no additional packages except the ones that are loaded with profvis, e.g. htmlWidgets and htmltools.
To be more specific I imagine something like:
library(profvis)
profvis_output <- profvis(rnorm(1e06))
htmlwidgets::saveWidget(profvis_output, "provis_output.html")
and then include the thereby created html file in Markdown.
I already tried
htmltools::includeHTML("profvis_output.html")
in the Markdown file but that did not work.
NOTE: I also want it to be renderable to a PDF
This seems to work:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(profvis)
```
## profvis
This is an R Markdown document using profvis.
```{r}
profvis({
data(diamonds, package = "ggplot2")
plot(price ~ carat, data = diamonds)
m <- lm(price ~ carat, data = diamonds)
abline(m, col = "red")
})
```

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)
```