在DT行中选择传单上的标记,然后单击,反之亦然 [英] selecting a marker on leaflet, from a DT row click and vice versa

查看:112
本文介绍了在DT行中选择传单上的标记,然后单击,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的最小示例呈现了一个包含3个市场的传单地图和一个包含3条记录的DT表.选择地图上的市场时,表上的匹配记录也将被选择.但是,我不能做的是也有相反的情况,在表上单击的行还会在地图上显示相关的弹出窗口.

The minimal example below renders a leaflet map with 3 markets, and a DT table with 3 records. When a market on the map is selected, so to is the matching record on the table. However, what I cannot do, is to also have the reverse of that, where a clicked row on the table also shows the related popup on the map.

我一直找不到能够执行类似操作的示例R闪亮传单应用程序.

I have been unable to find an example R shiny leaflet app that does something similar.

调整了代码以反映最初的评论

CODE tweaked to reflect initial comments

    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)

推荐答案

这不是解决方案,只是我在查看代码时发现的一些内容.

This is not a solution, just some things that I found out about the code when looking at it.

  1. ID plantRanks在您的代码中仅出现一次.
  1. The ID plantRanks only appears once in your code.

input$plantRanksDT_rows_selected中.这些东西很容易找到并且易于修复.正确的ID应该是数据表的输出ID,因此ranksDT.更换后,您将看到第二个问题

That is in input$plantRanksDT_rows_selected. Such things are easy to find and easy to fix. The correct id should be the output id of the datatable, so ranksDT. Once you replace that, you will see a second issue

  1. proxy2 %>% opsMap_marker_click$id <- a没有道理.
  1. proxy2 %>% opsMap_marker_click$id <- a makes no sense.

input$opsMap_marker_click$id存在,但显然不能写入.我不完全了解leaflet代理的工作原理,但是

input$opsMap_marker_click$id exists but can obviously not be written. I don't exactly know how leaflet proxys work, but

leaflet::addMarkers()

看起来很有希望.祝你好运!

looks promising. Good luck!

这篇关于在DT行中选择传单上的标记,然后单击,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆