Search code examples
javascripthtmlrrstudiodygraphs

How to embed a functional dygraph exported from R in HTML


Contrary to this similarly titled SO post, I have a dygraph I have exported from R that works. The graph was "exported" using rStudio's Viewer Export pictured below. This creates an HTML file of the dygraph you can confirm works in this fiddle

enter image description here

I tried to embed the entire dygraph at the top of another HTML using the recommendation from this SO post to no success - literally nothing happened. In another attempt I copy and pasted the dygraph HTML into the host HTML to an improved but still terrible outcome of having the graph overlap all content.

I can't seem to find an official way of embedding r dygraphs in HTML that isn't Shiny related or R Markup related or involves outputting CSV and respecifying the graph in HTML. Is there an official way of embedding r produced dygraphs in HTML and if so what is it? If not, how can I?

dygraphs are awesome

Solution

  • Because the dygraph is a standalone page, the easiest way to do what you want is to use an iframe: just set its src attribute to point to the HTML saved from RStudio. Here's one that embeds the dygraph you made in your fiddle:

    <h1>Here's a nice dygraph.</h1>
    <iframe src="https://jsfiddle.net/f1fLLjbk/embedded/result/" width="100%" height="300px" style="border: 0px;"/>
    
    <h1>Here's some more content</h1>
    

    Try it: https://jsfiddle.net/cvekdbLh/