【问题标题】:How to get the rows corresponding to a plot selection in shiny如何获取与闪亮的绘图选择相对应的行
【发布时间】:2019-07-20 19:57:20
【问题描述】:

我有一个条形图,它是闪亮应用程序的一部分。我用 plotly 创建了它。我希望用户能够选择图表的一部分(单击),并在单击数据表时显示与图表该部分悬停文本中给出的值相对应的所有行。

到目前为止,我能够显示 event.data 的输出,这不是很有趣。如何显示原始表中的相关行?

library(plotly)
library(shiny)

ui <- fluidPage(
  uiOutput("ChooserDropdown"),
  plotlyOutput("plot2"),

  DT::dataTableOutput("tblpolypDetail2")
)

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

  output$plot2 <- renderPlotly({
    # use the key aesthetic/argument to help uniquely identify selected observations
    #key <- row.names(mtcars)
browser()
      p <- ggplot(iris,aes_string(iris$Species,input$Chooser)) + geom_col()
      ggplotly(p,source = "subset") %>% layout(dragmode = "select")

  })
  output$tblpolypDetail2 <- renderDataTable({
    event.data <- event_data("plotly_click", source = "subset")
    print(event.data)
  })


output$ChooserDropdown<-renderUI({
  selectInput("Chooser", label = h4("Choose the endoscopic documentation column"),
              choices = colnames(iris) ,selected = 1
  )
})

}
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我创建了一个小演示,您可以通过单击绘图来突出显示数据表中的行。


    您需要分两步完成:

    1. 将点击的pointNumber映射到datatable()中的行,你可以为它创建一个外部表。
    2. 您需要创建一个dataTableProxy,您可以在其中更新数据表
    library(plotly)
    library(DT)
    library(shiny)
    library(dplyr)
    
    data <- as_tibble(iris) %>%
        group_by(Species) %>%
        summarise(avg = mean(Sepal.Width)) %>%
        mutate(Species = as.character(Species))
    
    species_mapping <- data.frame(
        Species = data$Species,
        row_id = 1:length(data$Species),
        stringsAsFactors = FALSE
    )
    
    ui <- fluidPage(
        DT::dataTableOutput("table"),
        plotlyOutput("plot")
    )
    
    server <- function(input, output, session) {
    
        output$plot <- renderPlotly({
             p <- data %>%
                ggplot() +
                geom_col(aes(x = Species, y = avg))
             # register this plotly object
             plotly_object <- ggplotly(p,source = "source1")
             event_register(plotly_object,event = "plotly_click")
             plotly_object
    
        })
    
        output$table <- DT::renderDataTable(data)
    
        # create a proxy where we can update datatable
        proxy <- DT::dataTableProxy("table")
    
        observe({
            s <- event_data("plotly_click",source = "source1")
            req(!is.null(s))
            # map point number to Species
            row_clicked <- species_mapping[s$pointNumber + 1,"row_id"]
            proxy %>%
                selectRows(NULL) %>%
                selectRows(row_clicked)
    
        })
    }
    shinyApp(ui, server)
    

    【讨论】:

    • 感谢@YifuYan。我希望显示的表是原始的 iris 表(而不是按物种分组),以便显示所有行(例如 setosa)。
    • 另外我不明白为什么我不能访问悬停文本可以访问的相同变量而不是引用 pointNumber...
    • 为了简单起见,我做了分组表。您可以通过更改映射表来映射到原始表。
    猜你喜欢
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2014-08-31
    • 2021-01-27
    • 2018-10-01
    相关资源
    最近更新 更多