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>

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.