我希望能够做类似这个答案但不使用闪亮的事情。我还想绑定打开与数据点关联的超链接的 onclick 事件。
我正在使用该saveWidget
函数,htmlwidgets
并且知道我可以使用包中的appendContent
函数插入 javascript 代码htmltools
。
这是一个小示例代码:
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小部件中。