从DT行单击选择小册子上的标记,反之亦然

时间:2017-06-29 20:37:57

标签: r shiny leaflet dt

下面的最小示例呈现具有3个市场的传单地图和具有3个记录的DT表。当选择地图上的市场时,表格上的匹配记录也是如此。但是,我不能做的是,也就是说,表中的一个单击行也会在地图上显示相关的弹出窗口。

我一直无法找到一个类似的R闪亮传单应用程序。

CODE调整以反映初始评论

    library(shiny)
library(leaflet)
library(DT)
library(tidyverse)

# Define UI for application that draws a histogram
ui <- fluidPage(

    leafletOutput("opsMap"),
    DT::dataTableOutput('ranksDT')
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    lats <- c(21.608889,21.693056, 24.04)
    longs <- c(-74.650833, -73.095,-74.341944)
    popups <- c('a','b','c')
    layerids <- c('a','b','c')
    iconNames <- c('cog','cog','cog')
    iconColors <- c('red','red','red')

    sampleData <- data_frame(lats,longs, popups,layerids,iconNames,iconColors)

    score <- c(7,3,9)

    locationRanks <- data_frame(popups, score)

    output$opsMap <- renderLeaflet({

        leaflet() %>%
            addTiles() %>% 
            addAwesomeMarkers(lat = sampleData$lats, 
                              lng = sampleData$longs, 
                              popup = sampleData$popups, 
                              layerId = sampleData$layerids,
                              icon = makeAwesomeIcon(icon=sampleData$iconNames, 
                                                     markerColor=sampleData$iconColors))
    })

    output$ranksDT <- DT::renderDataTable({
        d1 <- datatable(locationRanks,
                        selection = 'single',
                        rownames=FALSE,
                        options = list(dom = 'tpi',
                                       pageLength =5,
                                       paging=FALSE,
                                       searching=FALSE
                        )
        )
        d1
    })

    # create a reactive value that will store the click position
    mapClick <- reactiveValues(clickedMarker=NULL)
    mapClick <- reactiveValues(clickedGroup=NULL)

    # create a reactive for the DT  table
    locationClick <-reactiveValues(clickedRow = NULL)

    # observe click events
    observe({
        mapClick$clickedMarker <- paste(input$opsMap_marker_click$id)
        mapClick$clickedGroup <- paste(input$opsMap_marker_click$group)
        locationClick$clickedRow <- input$ranksDT_rows_selected
    })

    # define a proxy variable for the plant rank table
    proxy1 = dataTableProxy('ranksDT')
    # when map is clicked, make the same table row selection - need row number
    observeEvent(input$opsMap_marker_click$id, {
        a <- which(locationRanks[1] == input$opsMap_marker_click$id)
        proxy1 %>% selectRows(a)
    })


    proxy2 = leafletProxy('opsMap', session = shiny::getDefaultReactiveDomain())
    # if table is clicked, select the same market from the map
    observeEvent(locationClick$clickedRow, {
        a <- as.character(locationRanks[locationClick$clickedRow,1])
        cat(file=stderr(),"clicked row", locationClick$clickedRow, a,'\n')
        #proxy2 %>% opsMap_marker_click$id <- a
    })


}

# Run the application 
shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:4)

如果要突出显示数据表中的行,解决方案可能是将 input$map01_marker_click$iddataTableProxy()selectRows()selectPage() 一起使用。

为了突出标记,我认为您可以使用一些 javascript 来模拟点击标记。但我也会采用更简单的方法来添加突出显示的标记并随后将其删除。

enter image description here

基本上你的问题在这个问题中得到了部分回答:Shiny - how to highlight an object on a leaflet map when selecting a record in a datatable?,剩下的部分在答案之一中。 -> 归功于他们。 由于代码相当冗长,我努力将其简化为可重现的最小示例。

最小可重复示例:

library(shiny)
library(leaflet)
library(DT)

qDat <- quakes[1:10, ]
qDat$id <- seq.int(nrow(qDat))

ui <- fluidPage(
  mainPanel(
    leafletOutput('map01'),
    dataTableOutput('table01')
  )
)

server <- function(input,output){
  
  output$table01 <- renderDataTable({
    DT::datatable(qDat, selection = "single", options = list(stateSave = TRUE))
  })
  
  # to keep track of previously selected row
  prev_row <- reactiveVal()
  
  # new icon style
  highlight_icon = makeAwesomeIcon(icon = 'flag', markerColor = 'green', iconColor = 'white')
  
  observeEvent(input$table01_rows_selected, {
    row_selected = qDat[input$table01_rows_selected, ]
    proxy <- leafletProxy('map01')
    proxy %>%
      addAwesomeMarkers(popup = as.character(row_selected$mag),
                        layerId = as.character(row_selected$id),
                        lng = row_selected$long, 
                        lat = row_selected$lat,
                        icon = highlight_icon)
    
    # Reset previously selected marker
    if(!is.null(prev_row())){
      proxy %>%
        addMarkers(popup = as.character(prev_row()$mag), 
                   layerId = as.character(prev_row()$id),
                   lng = prev_row()$long, 
                   lat = prev_row()$lat)
    }
    # set new value to reactiveVal 
    prev_row(row_selected)
  })
  
  output$map01 <- renderLeaflet({
    leaflet(data = qDat) %>% 
      addTiles() %>%
      addMarkers(popup = ~as.character(mag), layerId = as.character(qDat$id)) 
  })
  
  observeEvent(input$map01_marker_click, {
    clickId <- input$map01_marker_click$id
    dataTableProxy("table01") %>%
      selectRows(which(qDat$id == clickId)) %>%
      selectPage(which(input$table01_rows_all == clickId) %/% input$table01_state$length + 1)
  })
}

shinyApp(ui = ui, server = server)

答案 1 :(得分:0)

这不是一个解决方案,只是我在查看代码时发现的一些事情。

  1. ID plantRanks仅在您的代码中出现一次。
  2. 那是input$plantRanksDT_rows_selected。这样的东西很容易找到并且易于修复。正确的id应该是数据表的输出id,因此ranksDT。替换后,您将看到第二个问题

    1. proxy2 %>% opsMap_marker_click$id <- a毫无意义。
    2. input$opsMap_marker_click$id存在,但显然不能写。我不完全知道leaflet代理如何工作,但是

      leaflet::addMarkers()
      
      看起来很有希望。祝你好运!