0

我想在两个图 ( plotly) 中显示数据,并希望能够通过使用串扰在另一个图中显示一个图的选定点。可悲的是,我尝试的任何方法都不起作用。在服务器功能之外定义共享数据的解决方案不是一个选项,因为我的应用程序中的数据来自其他反应和输入。下面是一个代表。

library(shiny)
library(plotly)

ui <- fluidPage(
  sliderInput("rows", label = "# Rows", min = 50, max = 150, value = 100),
  plotlyOutput("scatter1"),
  plotlyOutput("scatter2")
)

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

  iris_new <- reactive({
    iris[1:as.numeric(input$rows),]
  })
  
  sd <- SharedData$new(iris_new)
  
  output$scatter1 <- renderPlotly({
    plot_ly(
      sd,
      x = ~Sepal.Length, 
      y = ~Sepal.Width,
      color = ~Species,
      type = "scatter",
      mode = "markers"
    )
  })
  
  output$scatter2 <- renderPlotly({
    plot_ly(
      sd,
      x = ~Petal.Length, 
      y = ~Petal.Width,
      color = ~Species,
      type = "scatter",
      mode = "markers"
    )
  })
}

shinyApp(ui, server)

我也尝试过做出SharedData$new(iris_new)反应式的表达,比如

iris_new <- reactive({
  SharedData$new(iris[1:as.numeric(input$rows),])
})

并使用iris_new()inplot_ly(...)但它也不起作用。我也试过sd$data(withSelection = T)没有运气。奇怪的是,当我选择一个点时,它可以工作(尽管我不能再取消选择)。但是当我尝试选择多个点(我真正想要的)时,另一个情节没有反应。

我需要这个来处理情节(而不是 d3scatter、scatterD3 等)!

4

1 回答 1

2

尝试这个

library(shiny)
library(plotly)

ui <- fluidPage(
    sliderInput("rows", label = "# Rows", min = 50, max = 150, value = 100),
    plotlyOutput("scatter1"),
    plotlyOutput("scatter2")
)

server <- function(input, output, session) {
    
    iris_new <- reactive({
        highlight_key(iris[1:as.numeric(input$rows),])
    })
    
    output$scatter1 <- renderPlotly({
        plot_ly(
            iris_new(),
            x = ~Sepal.Length, 
            y = ~Sepal.Width,
            color = ~Species,
            type = "scatter",
            mode = "markers"
        ) %>% 
            highlight("plotly_selected")
    })
    
    output$scatter2 <- renderPlotly({
        plot_ly(
            iris_new(),
            x = ~Petal.Length, 
            y = ~Petal.Width,
            color = ~Species,
            type = "scatter",
            mode = "markers"
        ) %>% 
            highlight("plotly_selected")
    })
}

shinyApp(ui, server)

按住鼠标选择一个区域,双击绘图取消选择。

于 2021-11-24T17:57:18.587 回答