I want to add labels like shown on http://rstudio.github.io/leaflet/popups.html. I like the default styling better than popups and that they work on hover rather than on click. However, I can't seem to figure out how to add multiple lines of text. Simply throwing in a <br/> doesn't work like it does for popups, and gets displayed with the rest of the string. If I wrap it in htmltools::HTML, e.g.
function1 <- function(v1, v2) htmltools:HTML(paste0("Variable 1: ", v1, "<br/> Variable 2: ", v2))
leaflet(data) %>% addPolygons(... , label = ~function1(var1, var2))
then for some reason does add the break but weirdly displays all the values rather than just for the one polygon. I haven't tried this with other types like markers.
(BTW are these Labels called Tooltips by Leaflet? Would help to know what documentation to look at for the possible labelOptions.)
Check http://rpubs.com/bhaskarvk/electoral-Map-2016 for example on how to put HTML content in a label.
Labels are called Tooltips in Leaflet 1.x JS , but we're still on 0.7.x where they are implemented via the Leaflet.labels plugins.
Thanks. Wow, that looks awesome. That works but seems a little bit cumbersome -- any plans to make it as simple as it is for popups?
Unfortunately the way it's easier to do in popups is because we didn't add some safety checks to the popup code that we did to labels code. But I'll double check.
Cool beans... One more formatting question -- I see how to change the font-family using the style argument to labelOptions, but how would you use a custom font sitting in a local directory? Note I'm not using Shiny for this particular use case (although it may eventually become a Shiny app).
You will have to add that as an HTML dependency.
So like discussed here by timelyPortfolio - http://stackoverflow.com/questions/35720698/is-it-possible-to-include-custom-css-in-htmlwidgets-for-r-and-or-leafletr?
Doing it that way, will the font be solely contained in the HTML file or will it require that the device it's being viewed on also has the font installed? Would you use the same approach for a Shiny app?
Generally will be contained in the HTML file. You can use the same approach for a Shiny app, yes.
Just want to share this, since it was helpful for me in solving this problem in a bit simpler fashion:
https://stackoverflow.com/questions/43144596/r-and-leaflet-how-to-arrange-label-text-across-multiple-lines
Thanks @colearendt, that solution helped me too.
Just wanted to note in case this is helpful to anyone else: a user called Nate in that SO post says that if 'labs' is already a comment in the data frame, you can still use ~ notation with HTML() to refer to that column instead of to an external vector. That didn't work for me, but I ended up just specifying df$labs (because I had labs as a df column) without the ~, which worked well.
Most helpful comment
Check http://rpubs.com/bhaskarvk/electoral-Map-2016 for example on how to put HTML content in a label.
Labels are called Tooltips in Leaflet 1.x JS , but we're still on 0.7.x where they are implemented via the Leaflet.labels plugins.