在手动重新排序 plotly parcoord 图的尺寸后希望触发 observeEvent

Looking to trigger an observeEvent after manually reordering the dimensions of a plotly parcoord plot

我正在使用 plotly parcoords 生成平行坐标图。现在,想法是当用户拖动列轴并手动更改图中维度的顺序时,我想生成一个文本,显示基于该列顺序的一些值。但我不确定该怎么做。我什至不确定这是否可能。我知道我必须使用 observeEvent,但不确定要观察什么。我对 R Shiny 很陌生。请帮忙!

UI:

fluidRow(
    textOutput(outputId = "PlotScoreText")),
fluidRow(
  plotlyOutput("ParallelChart"))

服务器:

observeEvent(input$ParallelChart, {
output$PlotScoreText <- renderText(getScoreText())})
output$ParallelChart <- renderPlotly({
  getParallelChart()
})

getParallelChart <- function() {
    
    p <- plot_ly(type = 'parcoords', line = list(color = 'blue'),
         dimensions = list(
           list(range = c(1,5),
                constraintrange = c(1,2),
                label = 'A', values = c(1,4)),
           list(range = c(1,5),
                tickvals = c(1.5,3,4.5),
                label = 'B', values = c(3,1.5)),
           list(range = c(1,5),
                tickvals = c(1,2,4,5),
                label = 'C', values = c(2,4),
                ticktext = c('text 1', 'text 2', 'text 3', 'text 4')),
           list(range = c(1,5),
                label = 'D', values = c(4,2))
           )
         )
    
    p
}

例如,在渲染上面的图之后,如果用户将维度 C 拖到 B 的前面,我希望触发输出 $PlotScoreText 的 observeEvent。有什么办法吗?

我们可以使用 plotly 的 event_data() 来访问当前轴顺序(修改顺序会导致重新样式事件):

library(shiny)
library(plotly)

ui <- fluidPage(
  fluidRow(textOutput(outputId = "PlotScoreText")),
  fluidRow(textOutput(outputId = "renderTextOutput")),
  fluidRow(plotlyOutput("ParallelChart"))
)

server <- function(input, output, session) {
  
  output$ParallelChart <- renderPlotly({
    p <- plot_ly(type = 'parcoords', line = list(color = 'blue'),
                 dimensions = list(
                   list(range = c(1,5),
                        constraintrange = c(1,2),
                        label = 'A', values = c(1,4)),
                   list(range = c(1,5),
                        tickvals = c(1.5,3,4.5),
                        label = 'B', values = c(3,1.5)),
                   list(range = c(1,5),
                        tickvals = c(1,2,4,5),
                        label = 'C', values = c(2,4),
                        ticktext = c('text 1', 'text 2', 'text 3', 'text 4')),
                   list(range = c(1,5),
                        label = 'D', values = c(4,2))
                 ), source = "pcoords_events") %>%
      event_register("plotly_restyle")
  })
  
  axesOrder <- reactiveVal(paste("Axes order:", paste(c(LETTERS[1:4]), collapse = ", ")))
  
  observeEvent(event_data("plotly_restyle", source = "pcoords_events"), {
    d <- event_data("plotly_restyle", source = "pcoords_events")
    axesOrder(paste("Axes order:", paste(d[[1]]$dimensions[[1]]$label, collapse = ", ")))
  })
  
  output$PlotScoreText <- renderText({
    axesOrder()
  })
  
  output$renderTextOutput <- renderText({
    d <- event_data("plotly_restyle", source = "pcoords_events")
    paste("renderTextOutput: Axes order:", paste(d[[1]]$dimensions[[1]]$label, collapse = ", "))
  })
}

shinyApp(ui, server)