3

Interactive graphs created with ggplotly() go along nicely with html_document output in R Markdown, see eg. RMarkdown and ggplotly. For github_document output, however, the knitted HTML preview file does not show ggplotly() graphs.

I adopted the code from the linked SO post and only changed the output format in the header. Does anyone know how to render plotly graphs correctly with this kind of output? Or at least, if that is even possible?

---
title: "RmarkdownExample"
author: "fdetsch"
date: "April 16, 2020"
output: github_document
---

Here is the graph I generated. 

```{r setup, message = FALSE, echo = FALSE, warning=FALSE}
# Require
library(plotly)
# Create
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
# Plot
g <- qplot(carat, price, data=dsamp, colour=clarity)
# Call
ggplotly(g)
```

enter image description here

2 Answers 2

2

For output: github_document, I found a workaround that renders ggplotly() graphs nicely using iframes. The trick is to export the plotly widget as HTML and subsequently embed it as iframe. In my opinion, the advantage over output: html_document with keep_md enabled is that the online .md file simply prints a link to the intermediary HTML file instead of the full widget code, making it much tidier.

---
title: "Render `ggplotly()` graphs in `github_document`"
author: "fdetsch"
date: "`r Sys.Date()`"
output: github_document
---

Here is the graph I generated. 

```{r setup, message = FALSE, echo = FALSE, warning = FALSE}
# Require
library(plotly)
# Create
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
# Plot
g <- qplot(carat, price, data=dsamp, colour=clarity)
# Call
p <- ggplotly(g)

```

```{r include, echo = FALSE}
htmlwidgets::saveWidget(p, "index.html")

htmltools::tags$iframe(
  src=file.path(getwd(), "index.html"),
  width="100%",
  height="600",
  scrolling="no",
  seamless="seamless",
  frameBorder="0"
)

```

At least when opening the preview HTML in an external viewer, the interactive graph shows up. The RStudio (preview version 1.3.938) viewer currently fails to render the image.

Sign up to request clarification or add additional context in comments.

Comments

1

There seem to be some problems with github_document, see here. My workaround: knit to html_document and save the resulting *.md-file. So the YAML header is:

---
title: "RmarkdownExample"
author: "fdetsch"
date: "April 16, 2020"
output: 
  html_document:
    keep_md: true
---

You can then use the md file to upload to github.

2 Comments

Thanks for pointing me towards this thread. I agree that the HTML renders nicely. However, the online .md looks quite messy.
True. I found a workaround for output: github_document and also shared the code. Honestly, I think that both answers have their justified place here, which makes it hard to accept one over the other..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.