How to add an html table to leaflet popup

2,557 views
Skip to first unread message

MySchizo Buddy

unread,
May 30, 2016, 11:46:20 AM5/30/16
to Shiny - Web Framework for R

One way is to use html table tags directly like below

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"

))

# Use html table tags. Ugly and prone to errors
leaflet
(df) %>% addTiles() %>%
  addMarkers
(~Long, ~Lat, popup = ~paste("<table>
                                            <thead><tr><td>Name</td><td>Longitude</td><td>Lattitude</td></tr></thead>
                                            <tbody><tr><td>"
, Name, "</td><td>", Long, "</td><td>", Lat, "</td></tr></tbody>
                                         </table"
))


 This works fine. However another way is to use htmlTable package and that is where I'm having trouble. htmlTable takes a dataframe and converts into an html table

# Use htmlTables
leaflet
(df) %>% addTiles() %>%
  addMarkers
(~Long, ~Lat, popup = ~htmlTable(data.frame(Name, Long, Lat)))

Joe Cheng

unread,
May 30, 2016, 3:34:44 PM5/30/16
to MySchizo Buddy, Shiny - Web Framework for R
Looks like you'd probably need to do lapply/sapply/vapply. You want popup to be a character vector, with one element per dataframe row.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/2dcd0f74-18e1-49e2-b90c-949537a14a56%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

MySchizo Buddy

unread,
May 30, 2016, 4:22:32 PM5/30/16
to Shiny - Web Framework for R, myschi...@gmail.com
Thanks. This worked for me
popup = apply(df, 1, function(row) htmlTable(row))


Message has been deleted

Joe Cheng

unread,
Jun 1, 2016, 2:40:00 PM6/1/16
to MySchizo Buddy, Shiny - Web Framework for R
seq_along(df) goes by columns, not rows (so with e.g. cars, you get 1:2 instead of 1:50). Maybe something like this instead?

df %>% leaflet() %>% addTiles() %>%
  addMarkers
(~Long, ~Lat, popup = lapply(rownames(df), function(row) htmlTable(df[row, ])))

On Wed, Jun 1, 2016 at 11:22 AM MySchizo Buddy <myschi...@gmail.com> wrote:
ok this is a bad solution
lapply traverses the df differently than leaflet does.

# Use htmlTables
df
%>% leaflet() %>% addTiles() %>%
  addMarkers
(~Long, ~Lat, popup = lapply(seq_along(df), function(row) htmlTable(df[row, ])))

For large dataframes the lat and long are correct but you can see completely wrong rows in the popups.


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Tiernan Martin

unread,
Dec 7, 2016, 2:19:14 PM12/7/16
to Shiny - Web Framework for R, myschi...@gmail.com
Is there a way this can be done using DT::datatable() instead of htmlTable()?

How would I extract the HTML string from the html widget that datatable() creates?

Thanks!
Reply all
Reply to author
Forward
0 new messages