3

I am using R htmlwidget package, which save plotly graph in the form of html.The function htmlwidget::savewidget()save graph as a html file. Now we need to include a custom js file to the plotly html file generated by htmlwidget::savewidget() for additional operation, like clicking on the graph etc ... e.g My custom.js file contains the following code.

<script type="text/javascript">
$( document ).ready(function() {
$('#htmlwidget_container').on('plotly_click', function(data){
alert('You clicked this Plotly chart!');
 });
 });
</script>
Qaiser iqbal
  • 306
  • 1
  • 14

1 Answers1

4

You can add javascript through htmlwidget function onStaticRenderComplete()

//////////////////////////////////////////////////
                     javascript <- HTML(paste("

                       //here write your own javscript 

                         ", sep=''))

                      //pass this  javascript to prepend function and assign 
                         it to your graph object.

                    p <- prependContent(p,onStaticRenderComplete(javascript))

                   htmlwidgets::saveWidget(p, plotlyoutput, selfcontained = 
                   FALSE)

Another method for external js file is ::

follow this link

Embedding an R htmlwidget into existing webpage

Qaiser iqbal
  • 306
  • 1
  • 14