繁体   English   中英

在R Shiny中使用Plotly在Datapoint上单击打开链接

[英]Open Link on Datapoint Click with Plotly in R Shiny

我正在R中与Plotly合作开发Shiny Web应用程序。 我整理了一个交互式散点图,并用我想要的信息配置了悬浮文本。

我的实现如下:

plot_ly(data=partition, 
          x=~get(x), 
          y=~get(y), 
          color=~SenderS,
          colors="Set1",
          text=~paste("Link(s): <a href='", partition$Link,"'>", partition$Link, "</a>",
                      "<br>Date: ", partition$Date,
                      "<br>Parties: ", partition$SenderS, " to ", partition$Target,
                      "<br>", x_og, ": ", partition[,x],
                      "<br>", y_og, ": ", partition[,y])
          )%>%
    layout(title=~paste(x_og, " vs. ", y_og, 
                        "<br>R=", cor(partition[,x], partition[,y])),
           xaxis=list(
             title=x_og
           ),
           yaxis=list(
             title=y_og
           ))

在弹出窗口中,我有一个链接,希望用户能够导航到该链接。 不幸的是,一旦用户将鼠标悬停在当前弹出窗口上,它就会消失,因为他们不再将鼠标悬停在该点上。

有没有一种方法可以配置Plotly悬浮文本,以便我的用户可以单击链接? 或者,也许我可以单击散点图中的一个点来打开链接?

这是一个散点图,其中的点在单击时会打开链接:

library(plotly)
library(htmlwidgets) # to use the 'onRender' function

dat <- iris[1:2,]
urls <- c("http://google.com", "https://stackoverflow.com")

p <- plot_ly(dat, type = "scatter", mode = "markers",
             x = ~Sepal.Width, y = ~Sepal.Length, 
             customdata = urls)

js <- "
function(el, x) {
  el.on('plotly_click', function(d) {
    var point = d.points[0];
    var url = point.data.customdata[point.pointIndex];
    window.open(url);
  });
}"

p %>% onRender(js)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM