Closed ghost closed 7 years ago
Here's an example of htmlwidgets being embedded/rendered into existing web pages on the fly with trelliscopejs (with the htmlwidgets having been created from R previously).
http://hafen.github.io/trelliscopejs-demo/gapminder_plotly/
Each panel in the display is a plotly htmlwidget being dynamically embedded in the page.
The json for each plot is stored in files here. And then as the user pages through the panels, this code is called to load the necessary json and render it in the application. Note this code is a small piece of a much more complex React application, so it won't be very useful to copy from.
It wasn't very seamless to write the javascript to embed into the webpage since htmlwidgets uses a more jQuery-like approach to rendering widgets on a page with the widget's data already in a script tag and I was doing this in a React application where I need to do it more explicitly. It would be nice to have the logic for embedding more modular so that there are general methods that can be called in either case.
Another approach I commonly use to embed htmlwidgets in other web pages is writing the widget out to a gist using rbokeh::widget2gist()
and then embed it using an iframe. See here for an rbokeh example: https://bl.ocks.org/hafen/d9dcb681a9a4a4ee1ae24525a4d25ca1. widget2gist()
, although part of the rbokeh package, works with any htmlwidget and will give you the necessary embed code.
@processadd, this post htmlwidgets | Look Ma No R might also help. I had played a little with a script that would convert a htmlwidget to react component but never spent the time to fully develop the idea.
Thanks. These are all very impressive. I'm working on a solution to create widgets on web pages end to end. Hopefully I can finish that soon...
Hi, looks like all current solutions on htmlwidgets start from R env, like R markdown, shiny, etc. I'd like to ask whether there are solutions to use htmlwidgets in non-R env, e.g. run R on the fly, generate the htmlwidgets (or html) on the fly and embed/render it on existing web pages. Thanks