Open jbixon13 opened 4 years ago
@jbixon13 Are you generating the plots in an Rmarkdown file or in an R script? Can you provide a minimal reprex?
@jbixon13 I agree with Ramnath that reprex or just some additional details would be helpful to determine the best strategy. I thought I would mention though that you could easily strip the dependencies from the widget and provide these in the public assets in your react app. This post might be good reference.
I don't have time to fully explain it/provide examples right now but you might want to not use saveWidget
but instead the lower-level htmltools primitives it's based on.
htmltools::renderTags
on each widget, each return value will have both HTML and a list of dependencies.htmltools::resolveDependencies
.htmltools::copyDependencyToDir
and makeDependencyRelative
to place the dependencies somewhere on disk relative to your final .html.htmltools::renderDependencies
to generate the actual HTML for the dependencies.See https://github.com/rstudio/htmltools/blob/e60e803679eaae7475e18693a0d77b48dda8f908/R/html_print.R#L71 for how htmltools::save_html
(which saveWidget
is based on) does it.
If you would like to suppress dependencies that you are already providing to the .html page some other way, you can do so using htmltools::suppressDependencies
and adding them to the list of dependencies you pass to resolveDependencies
.
@jcheng5 This might be a worthwhile utility function to add to htmlwidgets
. I will take a crack at this next week, based on the logic you have outlined.
Thank you for the different approaches, I am generating the plots in an R script. Please see the following details:
I am exporting from R with:
plt_ridership_viz <- ggplotly(plt_ridership) %>%
layout(title = list(text = 'There is no clear trend of monthly ridership',
font = list(size = 15)
)
) %>%
config(displayModeBar = FALSE, scrollZoom = FALSE) %>%
htmlwidgets::saveWidget(file = 'plotly_ridership.html', selfcontained = FALSE, libdir = 'plotly_files')
It compiles plotly_ridership.html and plotly_otp.html (the same file but renamed) with the following dependencies listed in the <head>
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<style>body{background-color:white;}</style>
<script src="plotly_files/htmlwidgets-1.5.1/htmlwidgets.js"></script>
<script src="plotly_files/plotly-binding-4.9.1/plotly.js"></script>
<script src="plotly_files/typedarray-0.1/typedarray.min.js"></script>
<script src="plotly_files/jquery-1.11.3/jquery.min.js"></script>
<link href="plotly_files/crosstalk-1.0.0/css/crosstalk.css" rel="stylesheet" />
<script src="plotly_files/crosstalk-1.0.0/js/crosstalk.min.js"></script>
<link href="plotly_files/plotly-htmlwidgets-css-1.49.4/plotly-htmlwidgets.css" rel="stylesheet" />
<script src="plotly_files/plotly-main-1.49.4/plotly-latest.min.js"></script>
<title>plotly</title>
</head>
This is then moved to the public directory of my React application and called with the following:
<Scrolly>
<section>
<figure>
<iframe title='MTA ridership plot' frameBorder='0' src='../plot_assets/MTA_article/plotly_ridership.html'/>
</figure>
<article>
<div>
<p>This is a plotly chart</p>
</div>
</article>
</section>
</Scrolly>
<Scrolly>
<section>
<figure>
<iframe title='MTA on-time performance plot' frameBorder='0' src='../plot_assets/MTA_article/plotly_otp.html'/>
</figure>
<article>
<div>
<p>This is a plotly chart</p>
</div>
</article>
</section>
</Scrolly>
The following is a screenshot of my application with both plots rendering:
You can also see here that the JS dependencies are imported twice:
I am trying to embed multiple Plotly htmlwidgets in a React frontend. My goal is to reduce the file size of each plot based on the Plotly documentation:
Based on this, I have exported my plotly objects:
When I embed these plots (stored in the public folder of my React app) and render them to the DOM, the calls to the JS/CSS dependencies (
htmlwidgets
,jquery
,plotly
, etc.) are duplicated for every plot even thoughselfcontained = FALSE
is meant to avoid this. I tried removing the<script>
tags from the subsequent html files to avoid reading in packages that were already fetched by the first file, but this resulted in the subsequent plot not rendering. Is there something I'm missing in order to avoid duplicating these dependency calls?