将自定义 js 文件添加到 R html 小部件输出?

Add custom js file to R html widget output?

我正在使用 R html 小部件包,它以 html.The 函数 htmlwidget::savewidget() 将图形保存为 html 文件的形式保存绘图。现在我们需要将自定义 js 文件包含到由 htmlwidget::savewidget() 生成的 plotly html 文件中以进行其他操作,例如单击图表等... 例如,我的 custom.js 文件包含以下代码。

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

可以通过htmlwidget函数onStaticRenderComplete()

添加javascript
//////////////////////////////////////////////////
                     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)

另一种获取外部js文件的方法是::

关注这个link