将onclick open hyperlink事件添加到在R中创建的html小部件 [英] Add onclick open hyperlink event to an html widget created in R
问题描述
我希望能够像这样做的答案没有使用闪亮。我还想绑定打开与数据点关联的超链接的onclick事件。
I would like to be able to do something like the answer of this but without using shiny. I also want to bind onclick events which open a hyperlink associated with the data point.
我正在使用 saveWidget
函数来自 htmlwidgets
并知道我可以从 htmltools中插入带有
appendContent
函数的javascript代码
包。
I am using the saveWidget
function from htmlwidgets
and know that I can insert javascript code with the appendContent
function from the htmltools
package.
这是一个小样本代码:
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小部件中。
Now obviously I am asking for help for the correct java script code to save in the 'javascript' variable which I then could integrate with appendContent into the html widget.
推荐答案
On e方式是
- 通过
onStaticRenderComplete
添加Javascript代码以执行一旦情节呈现 - Javascript事件是对找到的示例的简单修改这里
- 网址是通过
window.open
- add the Javascript code via
onStaticRenderComplete
in order to execute it once the plot is rendered - the Javascript event is a simple modification of the example found here
- the URL is opened via
window.open
对于具有不同痕迹的一般解决方案,请参阅:点击ggplot / plotly图表打开超链接
For a general solution with different traces, see: 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)
这篇关于将onclick open hyperlink事件添加到在R中创建的html小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!