【问题标题】:Shiny with two plotly plots and crosstalk issue闪亮的两个情节和串扰问题
【发布时间】:2022-01-03 00:35:44
【问题描述】:

我想在两个绘图 (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),])
})

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

我需要它来处理 plotly(而不是 d3scatter、scatterD3 等)!

【问题讨论】:

    标签: r plotly crosstalk


    【解决方案1】:

    试试这个

    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)
    

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

    【讨论】:

    • 工作就像一个魅力,ty
    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 2021-05-10
    • 2016-03-25
    • 2014-09-12
    • 2021-12-18
    • 2016-03-06
    • 2018-04-03
    • 2017-08-12
    相关资源
    最近更新 更多