在 R Shiny 中使用 Plotly 打开 Datapoint 上的链接 [英] Open Link on Datapoint Click with Plotly in R Shiny

查看:20
本文介绍了在 R Shiny 中使用 Plotly 打开 Datapoint 上的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 R 中的 Plotly 开发一个闪亮的网络应用程序.我整理了一个交互式散点图,并用我想要的信息配置了悬停文本.

I'm working with Plotly in R to develop a Shiny web app. I've put together an interactive scatterplot and configured the hovertext with the information I want.

我的实现如下:

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

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

In the popup, I have a link to which I would like the user to be able to navigate. Unfortunately, as soon as the user hovers over the current popup, it disappears as they are no longer hovering over the point.

有没有办法配置 Plotly hovertext 以便我的用户能够点击链接?或者,也许,我可以单击散点图中的一个点打开链接吗?

Is there a way to configure Plotly hovertext such that my user would be able to click the link? Or, perhaps, can I make clicking a point in the scatterplot open the link?

推荐答案

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

Here is a scatterplot with points that open a link when they are clicked:

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)

这篇关于在 R Shiny 中使用 Plotly 打开 Datapoint 上的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆