【问题标题】:Add onclick open hyperlink event to an html widget created in R将 onclick 打开超链接事件添加到在 R 中创建的 html 小部件
【发布时间】:2017-02-08 07:27:18
【问题描述】:

我希望能够做类似the answer of this 但不使用闪亮的事情。我还想绑定打开与数据点关联的超链接的 onclick 事件。

我正在使用来自htmlwidgetssaveWidget 函数,并且知道我可以使用来自htmltoolsappendContent 函数插入javascript 代码 包。

这是一个小示例代码:

library(ggplot2)
library(plotly)
library(htmlwidgets)
library(htmltools)

path.test.results <- "C:\\Users\\img\\"

myData <- data.frame(x=c(1,2,3), y=c(3,2,1))
myLinks <- c("https://www.google.com/", "https://stackoverflow.com/", "https://www.r-project.org/")

ggp <- ggplot(data=myData, aes(x=x, y=y)) + geom_point()
ply <- plotly_build(ggp)

ply$elementId <- "PlotlyGraph"

#javascript <- HTML('<script>document.getElementById("htmlwidget_container").innerHTML = "test";</script>')
javascript <- HTML(paste(
        paste('<button type="button" onclick="document.getElementById(',"'", 'PlotlyGraph', "'", ').style.display=',
                "'", 'none', "'", '">Hide Plot</button>', sep=''),
        paste('<button type="button" onclick="document.getElementById(',"'", 'PlotlyGraph', "'", ').style.display=',
                "'", 'block', "'", '">Show Plot</button>', sep='')
        ,sep=''))

ply <- appendContent(ply, javascript)

saveWidget(widget=ply, file=paste(path.test.results, "test.html", sep=""), selfcontained = FALSE)
dev.off()

现在显然我正在寻求帮助以将正确的 java 脚本代码保存在“javascript”变量中,然后我可以将其与 appendContent 集成到 html 小部件中。

【问题讨论】:

    标签: javascript r hyperlink plotly htmlwidgets


    【解决方案1】:

    一种方法是

    • 通过 onStaticRenderComplete 添加 Javascript 代码,以便在绘制绘图后执行它
    • Javascript 事件是对找到的示例 here 的简单修改
    • URL 是通过window.open 打开的

    对于不同痕迹的通用解决方案,请参阅:Open hyperlink on click on an ggplot/plotly chart

    完整代码

    library(ggplot2)
    library(plotly)
    library(htmlwidgets)
    library(htmltools)
    
    path.test.results <- "C:\\Users\\img\\"
    
    myData <- data.frame(x=c(1,2,3), y=c(3,2,1), urls=c("https://www.google.com/", "http://stackoverflow.com/", "https://www.r-project.org/"))
    myLinks <- c("https://www.google.com/", "http://stackoverflow.com/", "https://www.r-project.org/")
    
    ggp <- ggplot(data=myData, aes(x=x, y=y)) + geom_point()
    ply <- plotly_build(ggp)
    
    ply$elementId <- "PlotlyGraph"
    
    html <- HTML(paste(
      paste('<button type="button" onclick="document.getElementById(',"'", 'PlotlyGraph', "'", ').style.display=',
            "'", 'none', "'", '">Hide Plot</button>', sep=''),
      paste('<button type="button" onclick="document.getElementById(',"'", 'PlotlyGraph', "'", ').style.display=',
            "'", 'block', "'", '">Show Plot</button>', sep='')
      ,sep=''))
    
    javascript <- HTML(paste("var myPlot = document.getElementById('PlotlyGraph');
    myPlot.on('plotly_click', function(data){
    var urls = ['", paste(myLinks, collapse = "', '"), "'];
    window.open(urls[data.points[0].pointNumber],'_blank');
    });", sep=''))
    
    ply <- prependContent(ply, html)
    ply <- prependContent(ply, onStaticRenderComplete(javascript))
    
    saveWidget(widget=ply, file=paste(path.test.results, "test.html", sep=""), selfcontained = FALSE)
    

    【讨论】:

    • 太棒了!完全按照我的想法去做。非常感谢,你帮了我很多忙!
    • 我现在尝试将相同的技术应用于堆叠条形图。如果我在ggplot 通话中将x=x, y=y 之外的所有内容都排除在aes() 之外,那么一切正常。但是当我尝试添加例如, fill=urlsaes() 内似乎搞乱了 JavaScript 功能。然后总是会打开相同的链接(在我的情况下,第一个是谷歌),无论我点击哪个栏。你可能知道发生了什么事吗?
    • 抱歉,之前没遇到过这个问题。请提出一个单独的问题,我相信有人有解决方案。
    【解决方案2】:

    对马克西米利安彼得斯代码的轻微更正,这对我来说并不完全适用于一个因素(x)。我不是 Javascript 程序员,所以无法真正解释它(也许有人可以?),只是使用了情节示例。

    (我用 cmets 添加了 2 行)

    javascript <- HTML(paste("var myPlot = 
    document.getElementById('PlotlyGraph');
                         myPlot.on('plotly_click', function(data){
                         var urls = ['", paste(myLinks, collapse = "', '"),'];//added
                         for(var i=0; i < data.points.length; i++){
                         window.open(urls[data.points[i].x],'_blank'); //changed
                         }//added
                         });", sep=''))
    

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 1970-01-01
      • 2011-12-24
      • 2021-04-30
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 1970-01-01
      相关资源
      最近更新 更多