【问题标题】:Add custom js file to R html widget output?将自定义 js 文件添加到 R html 小部件输出?
【发布时间】:2017-08-17 09:49:00
【问题描述】:

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

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

【问题讨论】:

    标签: r plotly htmlwidgets


    【解决方案1】:

    可以通过htmlwidget函数添加javascriptonStaticRenderComplete()

    //////////////////////////////////////////////////
                         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文件的另一种方法是::

    点击此链接

    Embedding an R htmlwidget into existing webpage

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多