将 onclick 打开超链接事件添加到在 R 中创建的 html 小部件

Add onclick open hyperlink event to an html widget created in R

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

我正在使用 htmlwidgets 中的 saveWidget 函数并且知道我可以使用 htmltools 中的 appendContent 函数插入 java 脚本代码 包。

这是一个小示例代码:

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://whosebug.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 小部件中。

一种方法是

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

不同轨迹的通用解决方案,参见:

完整代码

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://whosebug.com/", "https://www.r-project.org/"))
myLinks <- c("https://www.google.com/", "http://whosebug.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)

对 Maximilian Peters 代码的轻微更正,该代码对我来说不太适合 factor(x)。我不是 Javascript 程序员,所以无法真正解释它(也许有人可以?),只是使用了情节示例。

(我添加了注释的两行)

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=''))