plotly / plotly/plotly.R

Feature Request: Remove traces by name using plotlyProxy()

未关闭
#1,248 3 条评论 5 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
R
星标
2.7k
派生
641
PR 合并指标
30 天内没有已合并 PR

描述

I originally posted this as a long shot stack overflow question, but I'm suspecting the functionality I'm looking for might require some new code.

When using plotlyProxy(), adding new traces with a name attribute is very straightforward. However, removing a trace by name does not appear to be possible. Reviewing the plotly.Js function reference, it appears that the JavaScript methods exclusively use indices, so this might require an additional javascript method to match names to trace numbers.

The example shiny app below illustrates the functionality I'm looking for. In the screenshot, when the user clicked the "Remove Trace" button, trace xxx would be removed.

plotlyproxy

library(shiny)
library(plotly)

ui <- fluidPage(
  textInput("TraceName", "Trace Name"),
  actionButton("Add","Add Trace"),
  actionButton("Remove","Remove Trace"),
  plotlyOutput("MyPlot")
)

server <- function(input,output,session) {

  ## Creating the plot
  output$MyPlot <- renderPlotly({
    plot_ly() %>%
      layout(showlegend  = TRUE)
  })

  ## Adding traces is smooth sailing
  observeEvent(input$Add,{
    plotlyProxy("MyPlot", session) %>%
      plotlyProxyInvoke("addTraces", list(x = rnorm(10),y = rnorm(10),
                                          type = "scatter",mode = "markers",
                                          name = input$TraceName))
  })

  ## Ideal Solution (that does not work)
  observeEvent(input$Remove,{
    plotlyProxy("MyPlot", session) %>%
      plotlyProxyInvoke("deleteTraces", input$TraceName)
  })

}

JavaScript is not my strong suit, but it seems like one way to support this would be to add a new method in htmlwidgets/plotly.js, perhaps called traceNamesToIDs. Then, the steps might look something like the following.

DeleteTraceNames <- c("xxx")
MyPlotProxy <- plotlyProxy("MyPlot", session)
TraceNumberList <- plotlyProxyInvoke(MyPlotProxy, "traceNamesToIds", )) ## Returns c(0)
plotlyProxyInvoke(MyPlotProxy ,"deleteTraces", TraceNumberList )

I'm not quite sure, but as I read through htmlwidgets/plotly.js, it seems like there is already some very similar functionality in the codebase to support Cross Talk integration.

Alternatively, if the deleteTraces method could be modified to directly accept names as a character input, that seems like the most intuitive usage from a user standpoint. However, this might result in breaking changes for users who wrote code based on trace numbers depending on the details of implementation.

plotlyProxy("MyPlot", session) %>%
    plotlyProxyInvoke("deleteTraces", c("xxx"))

If some implementation of this functionality could be added in future versions, I think it could make a substantial difference in the ease of use!

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先阅读 inst/htmlwidgets/plotly.js,尤其是 plotlyProxyInvoke 的处理方式,以及 issue 中提到的现有 Cross Talk 相关功能。然后复现所提供的 Shiny 应用,并比较 deleteTraces 按 trace 名称和按 trace 索引处理时的行为。完成的标准是:示例可以按名称删除选中的 trace,同时不破坏现有的基于索引的用法。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, r
领域
data-visualization
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。