2

I am trying to create a leaflet map with a rendering table (depending on where you click on the map) to upload it to our company's confluence page. The first thing that came to my mind was to use Shiny to create a small web app but my company does not have R-Server. The next option I could think of is to use leaflet and htmlwidgets to export the html. However, I am not sure whether it is possible to add a rendering table (instead of the usual popups). Is it possible? Are there (better) ways to achieve my goal?

Patrick Balada
  • 1,330
  • 1
  • 18
  • 37

2 Answers2

2

You can use htmlTable.

library(leaflet)
library(htmlTable)

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

table <- data.frame(a = 1:3, b= c("a", "b", "c"))

leaflet(df) %>% addTiles() %>%
  addMarkers(~Long, ~Lat, popup = htmlTable(table, rnames = F))

enter image description here

DJack
  • 4,850
  • 3
  • 21
  • 45
  • @ DJack Thank you for the idea - that would be one way to do it. I should have mentioned that the table will be medium-sized and hence I would prefer to get it as a separate object and not as popup. – Patrick Balada Apr 20 '18 at 09:50
  • As I far as I know, you can't do that without using a shiny app. FYI, you can deploy 5 shiny apps for free (http://www.shinyapps.io/#pricing). – DJack Apr 20 '18 at 10:04
  • @DJack how can i increase the width of the table itself not or the spacing between columns – John Smith Dec 27 '19 at 06:13
  • Is there a way to filter the content of the popup htmlTable by only those entries at that point (the point you click?) – wraymond Sep 10 '21 at 19:11
1

As Gregor said, you can do this with crosstalk and DT. For example:

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

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

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

bscols(leaflet(sdf) %>% addTiles() %>%
        addMarkers(~ Long, ~ Lat),
        datatable(sdf, width = "100%"))

enter image description here

If you select entries in the table, the markers will be highlighted in the map; if you select an area in the map, the table will be subsetted to the markers that are in it (as long as there's at least one).

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Is it possible to hide and show the table by button or ...? – Masoud Jul 06 '21 at 07:58
  • How to change weight and height of the map? I want to increase width and height of the map and decrease width of the table. – Masoud Jul 12 '21 at 05:54
  • @Masoud, you should post your questions as separate questions, not as comments. – user2554330 Jul 12 '21 at 10:28
  • Ok, I will do it asap. Here is the first question: https://stackoverflow.com/questions/40310530/show-and-hide-columns-in-datatable-upon-checkbox-selection – Masoud Jul 12 '21 at 10:54