0
votes

I'd like to link a leaflet map and a data table created by DT library by using crosstalk instead of shiny. So when I click any record on table side, the circle in the map will be highlighted. I know the lat and long are required to generate the leaflet map, but is there a way that the table side only have Name and Area columns there (not show lat and long)?

Here are my example code:

library(leaflet)
library(DT)
library(crosstalk)

df <- read.csv(textConnection(
  "Name,Lat,Long, area
  Samurai Noodle,47.597131,-122.327298,40
  Kukai Ramen,47.6154,-122.327157,30
  Tsukushinbo,47.59987,-122.326726,10"
))

df$Name <- as.character(df$Name)

sdf <- SharedData$new(df, ~df$Name)


pal <- colorNumeric("RdYlBu", df$area)

labels <- paste(sep = "<br/>",
                paste('Name: ', df$Name), 
                paste('Area: ', df$area))

d1 <- leaflet(sdf) %>% 
  addTiles() %>% 
  addCircleMarkers(~Long, 
                   ~Lat, 
                   radius = df$area,
                   color = ~pal(df$area),
                   fillColor = ~pal(df$area),
                   popup = labels,
                   fillOpacity = 1) %>% 
  addLegend("topright",
            title = "AREA",
            pal = pal,
            values = df$area,
            opacity = 1) 

d2 <- datatable(sdf, width = "100%") 

bscols(d1, d2)
2

2 Answers

0
votes

I figured out this question today.

What I need to do is create df_2 (remove log and lat), and create another SharedData object by using df_2, and adding group.

sdf_2 <- SharedData$new(df_2, ~df$Name, group = "data_subset")

Use sdf and sdf_2 to create leaflet map and datatable separately:

d1 <- leaflet(sdf) %>% addTiles() %>% addCircleMarkers(~Long, 
               ~Lat, 
               radius = df$area,
               color = ~pal(df$area),
               fillColor = ~pal(df$area),
               popup = labels,
               fillOpacity = 1) %>% addLegend("topright",
        title = "AREA",
        pal = pal,
        values = df$area,
        opacity = 1) 

d2 <- datatable(sdf_2, width = "100%")
bscols(d1, d2)
0
votes

You don't need another SharedData.

A quick solution is in datatable's options.

datatable(sdf, options=list(columnDefs = list(list(visible=FALSE, 
                                      targets=c(2,3))))) #positions