plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.55k stars 623 forks source link

rmarkdown without inline JS #1586

Closed nicolaskruchten closed 5 years ago

nicolaskruchten commented 5 years ago

When I knit an Rmarkdown file with a plot_ly plot, the resulting HTML is pretty big, because it inlines plotly.js. Is there a flag somewhere that I can use to get it to load plotly.js from the CDN instead?

cpsievert commented 5 years ago

You can put self_contained: false in the yaml of the Rmd to have it import a local file instead (see ?rmarkdown::html_document).

I think you can use partial_bundle() with local=FALSE to get an external link instead, but you probably to be careful you're not importing multiple versions.

nicolaskruchten commented 5 years ago

So I just tried it with self_contained: false and it still seems to output a 3.8MB HTML file with inlined data-uri Javascript. Did I need to do anything else as well or...?

here are the contents of my source file (with extra spaces between the backticks for formatting reasons ;)

---
title: "Untitled"
output: html_document
self_contained: false
---

` ` `{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
` ` `

` ` `{r pressure, echo=FALSE, message=FALSE, warning=FALSE}
library(plotly)
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
` ` `
cpsievert commented 5 years ago

It needs to be this way (self_contained is an argument to html_document):

---
title: "Untitled"
output: 
  html_document:
    self_contained: false
---
nicolaskruchten commented 5 years ago

Ah perfect, thanks!

nicolaskruchten commented 5 years ago

Following up on this: is there any way to get the resulting HTML to request plotly.js from a CDN?

cpsievert commented 5 years ago

I think the only 'official' way to do it is to add partial_bundle(p, local = FALSE) to a plot p

nicolaskruchten commented 5 years ago

OK thanks :)