【问题标题】:Open Link on Datapoint Click with Plotly in R Shiny在 R Shiny 中使用 Plotly 在 Datapoint 上打开链接
【发布时间】:2019-09-28 18:56:25
【问题描述】:

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

我的实现如下:

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 hovertext 以便我的用户能够点击链接?或者,也许,我可以单击散点图中的一个点打开链接吗?

【问题讨论】:

    标签: r shiny plotly r-plotly


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 很棒的解决方案!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 2021-09-03
    • 1970-01-01
    • 2016-09-12
    • 2017-06-22
    • 2018-08-14
    • 2016-09-13
    • 1970-01-01
    相关资源
    最近更新 更多