【问题标题】:How to add a new trace while removing the last one?如何在删除最后一个跟踪时添加新跟踪?
【发布时间】:2021-11-04 13:13:39
【问题描述】:

我是新来的闪亮和阴谋。我要做的是先添加一个跟踪,然后我希望每次单击按钮时都将其替换为新跟踪。

这是我的最小示例:

library(shiny)
library(plotly)

ui <- fluidPage(plotlyOutput("fig1"),
                
                numericInput("A",
                             label = h5("A"),
                             value = "",
                             width = "100px"),
                numericInput("B",
                             label = h5("B"),
                             value = "",
                             width = "100px"),
                actionButton("action3", label = "Add to plot"),
                actionButton("action4", label = "Remove point")
                
                
                )
server <- function(input, output) {
  
  
  A <- 1:5
  B <- c(115, 406, 1320, 179, 440)
  data <- data.frame(A, B)
  
  fig <- plot_ly(data, x = A, y = B, type = 'scatter', mode = 'markers')
  
  output$fig1 <- renderPlotly(fig)
  
  observeEvent(input$action3, {
    vals <- reactiveValues(A = input$A, B = input$B)
    plotlyProxy("fig1") %>%
      plotlyProxyInvoke("addTraces", 
                        list(x = c(vals$A,vals$A),
                             y = c(vals$B,vals$B),
                             type = "scatter",
                             mode = "markers"
                             
                        )
      )
  })
  observeEvent(input$action4, {
    vals <- reactiveValues(A = input$A, B = input$B)
    plotlyProxy("fig1") %>%
      plotlyProxyInvoke("deleteTraces")
  })
}

shinyApp(ui,server)

我可以轻松地添加新的轨迹,但它们都保留在图上。 我的解决方案是添加一个新按钮来删除跟踪,但它不起作用。 我已经阅读了this,但我无法让它工作。

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    根据您的描述,听起来您想在按下按钮的同时添加跟踪并删除最近添加的跟踪。这仍然会留下您开始使用的原始图/轨迹。

    我试着简化了一下。第一个 plotlyProxyInvoke 将删除最近添加的跟踪(它是零索引,保留第一个 plotly 跟踪)。

    第二个plotlyProxyInvoke 将添加新的跟踪。请注意,基于this answer 包含两次 (x, y) 对。

    library(shiny)
    library(plotly)
    
    A <- 1:5
    B <- c(115, 406, 1320, 179, 440)
    data <- data.frame(A, B)
    
    ui <- fluidPage(plotlyOutput("fig1"),
                    numericInput("A",
                                 label = h5("A"),
                                 value = "",
                                 width = "100px"),
                    numericInput("B",
                                 label = h5("B"),
                                 value = "",
                                 width = "100px"),
                    actionButton("action3", label = "Add to plot"),
    )
    
    server <- function(input, output, session) {
      
      fig <- plot_ly(data, x = A, y = B, type = 'scatter', mode = 'markers')
      
      output$fig1 <- renderPlotly(fig)
      
      observeEvent(input$action3, {
        plotlyProxy("fig1", session) %>%
          plotlyProxyInvoke("deleteTraces", list(as.integer(1)))
        plotlyProxy("fig1", session) %>%
          plotlyProxyInvoke("addTraces", 
                            list(x = c(input$A, input$A),
                                 y = c(input$B, input$B),
                                 type = 'scatter',
                                 mode = 'markers')
          )
      })
      
    }
    
    shinyApp(ui,server)
    

    【讨论】:

    • 谢谢你,这正是我想做的,虽然我不明白你所说的列表(as.integer(1) 或“零索引”。
    • “零索引”是指与 plotly 一起使用的编号系统,从 0、1、2...开始,而不是 R,它是单索引或 1、2、3.. 。等等。因此,在这种情况下,您包括要删除的跟踪,因为它是零索引的,所以第一个跟踪是跟踪 0,第二个跟踪是跟踪 1,等等。由于您已经有了包含 5 个点的第一个跟踪,所以第二个跟踪是添加的单点,或删除的迹线 1。我注意到其他示例使用了as.integer(),所以我假设索引需要一个整数(技术上1是double而不是integer)......并且需要的结构是list......
    猜你喜欢
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    相关资源
    最近更新 更多