Writing r shiny leaflet |popup| - r

Any solution/help about writing r shiny leaflet |popup|.
Thinks
I am trying to put some informations into my R shiny leaflet tool, and I got a problem to write something like that:
popup = paste(data$url,data$nom)
The problem is that in the code the data$url is a website, and I want to render it like a link?
and when I try making with the ~symbol I got error messages ...
leaflet() %>%
addTiles(
urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
attribution = 'Maps by Mapbox'
) %>%
setView(lng = LONG, lat = LAT , zoom = ZOOM)%>%
addMarkers(data,lng=data$Longitude,lat=data$Latitude,
popup = paste(data$url,data$nom)
) %>%

You can use html tags for that. Just change your popup to the following:
popup = paste("",data$url,"",data$nom)

Related

can we popop the name of the place when we use addGeoJson in R

I'm using addgeoJson function for the india map. I have data that I got it from here
https://gadm.org/download_country.html
for example I'm using this data
This is a json file and I read that json file and pass it to addGeoJson function.
library(jsonlite)
library(leaflet)
url <- "https://raw.githubusercontent.com/openseattle/seattle-boundaries/master/data/zip-codes.geojson"
geojson <- fromJSON(url, simplifyVector = FALSE)
leaflet() %>%
addTiles() %>%
addGeoJSON(geojson) %>%
setView(lng = -122.2, lat = 47.6, zoom = 10)
So I need to popup the names when I click on a particular state or city here. It should give some information when we click on it.
Is this possible to do it? and how?
The information you are looking for is stored deep in the structure of a geojson file (list). See for instance the first object of your dataset
geojson$features[[1]]$properties
Luckily there is a easier way to deal with it.
Use the package leaflet.extra, function addGeoJSONv2 specifying your label under argument popupProperty
library(leaflet.extras)
library(jsonlite)
library(leaflet)
url <- "https://raw.githubusercontent.com/openseattle/seattle-
boundaries/master/data/zip-codes.geojson"
geojson_fil <- fromJSON(url, simplifyVector = FALSE)
leaflet() %>%
addTiles() %>%
setView(lng = -122.2, lat = 47.6, zoom = 10) %>%
addGeoJSONv2(geojson_fil,
popupProperty = 'ZCTA5CE10')

How to add Leaflet.Locate to an R made map (instead of locate)

I was looking into this repo, where a blue dot is added to your leaflet map, that if you open it on a phone, it follows you as you walk, similar to a google maps option, as shown in this demo.
Usually in R and using Rmd, I would have this code so that I can know where I am on the map:
title: "Test map"
output: html_document
knitr::opts_chunk$set(echo = TRUE)
library(leaflet)
Old map.locate option
I usually made this maps for the field
leaflet() %>%
addTiles() %>%
setView(-71.0382679, 42.3489054, zoom = 18) %>%
addEasyButton(easyButton(
icon="fa-crosshairs", title="Locate Me",
onClick=JS("function(btn, map){ map.locate({setView: true, enableHighAccuracy: true }); }")))
That works great for finding your location, but it does not generate a marker where you are, and more importantly, it does not follow you around, you can see an example of that here
Trying to incorporate control.locate
So my first try was just to change locate for control.locate, but that did not work.
leaflet() %>%
addTiles() %>%
setView(-71.0382679, 42.3489054, zoom = 18) %>%
addEasyButton(easyButton(
icon="fa-crosshairs", title="Follow Me",
onClick=JS("function(btn, map){ map.control.locate()}")))
I am still thinking of other options, but any help would be welcome, here is the full rmd in github
These GPS features have been implemented in the leaflet.extras package.
Here's a working version based on your MWE
library(leaflet)
library(leaflet.extras)
your_map <- leaflet() %>%
addTiles() %>%
setView(-71.0382679, 42.3489054, zoom = 18) %>%
addControlGPS(
options = gpsOptions(
position = "topleft",
activate = TRUE,
autoCenter = TRUE,
setView = TRUE))
activateGPS(your_map)
The results looks like this:
Spoiler: Chrome thinks I'm in São Paulo right now... (where I'd rather be!)
...and here's a working demo on my Git.
Work's like a charm on my mobile.

R: Using addResourcePath for rendering local leaflet tiles

I would like to add local tiles for leaflet to render them offline in a shiny application.
Although there are solutions to this on SO for example here and here , I am still ending up with grey map with no tiles. It would really help me to see some reproducible example.
Thanks.
My example code:
library(shiny)
library(dplyr)
library(RgoogleMaps)
#downloads tiles for a given regions, saves it to C:/Users/.../mapTiles/OSM
for (zoom in 0:16)
GetMapTiles(center = c(lat = 52.431635, lon = 13.194773),
zoom = zoom,
nTiles = round(c(20,20)/(17-zoom)))
#shiny ui
ui = fluidPage(leafletOutput("map"))
#create basic map, load tiles from directory and set view to centre of downloaded tiles
server = function(input, output, server){
addResourcePath(prefix = "OSM", "C:/Users/.../mapTiles")
output$map = renderLeaflet({
leaflet() %>%
addTiles( urlTemplate = "/OSM/{z}_{x}_{y}.png") %>%
setView(52.431635, 13.194773 , zoom = 10) %>% #set to the location with tiles
addMarkers(52.431635, 13.194773 )
}
)
}
shinyApp(ui, server)
In my case, I create my own tiles via gdal2tiles, which takes your data and automatically creates a {z}/{x}/{y}.png folder structure. Please see this link for a nice tutorial and what i mean about the file structure;
+---14
| +---8185
| +---5460.png
| +---5461.png
| +---etc.png
| \---8186
# I use the following server (see how my addTiles has a folder structure)
server <- function(input, output,session) {
addResourcePath("mytiles", "C:/.../tiles")
output$tilemap <- renderLeaflet({
leaflet() %>%
setView(lng = -4.4, lat = 52, zoom = 12) %>%
addTiles(urlTemplate = "mytiles/{z}/{x}/{y}.png")
})
}
Now, as you are downloading tiles from Google Maps to your hard drive, you'll want a slightly different approach as the files are downloaded in a {z}_{x}_{y}.png format, and not produced into a file structure like gdal creates;
+---11_1098_671.png
etc.
so you need to adjust your addTiles code to reflect this, using underscores, like the Google filenames;
server <- function(input, output,session) {
addResourcePath("mytiles", "C:/.../OSM")
output$tilemap <- renderLeaflet({
leaflet() %>%
setView(lng = 13.194773, lat = 52.431635, zoom = 11) %>%
addTiles(urlTemplate = "mytiles/{z}_{x}_{y}.png")
})
}
Lastly, my setView arguments are in a different order to yours but i'm not sure whether that makes a difference or not.
i tried this solution but it could not work,the topic is old but it really helped me to achieve what i wanted to do, i found another solution for those of you in the same case by creating two ports :
just define two differents ports for your shiny server( 3838) and for the server hosting the tiles (8000)
servr::httd(dir="C:/TestApp/data_hydrepat/tiles_hydrepat/mapTiles/mytiles",daemon=TRUE,port=8000)
options(shiny.port = 3838)
to close the server hosting the tiles, just put a reactive on an input or something.. and close
(servr::daemon_stop(which = daemon_list())
hope it'll help !

Way to add hyperlink to Leaflet popup in Shiny

Using leaflet in shiny to make an interactive map. Pulling data for popups from a CSV:
Row on CSV:
Name lat lng
Tufts 42.349598 -71.063541
Code on R for markers:
m %>% addMarkers(~lng, ~lat, icon = custommarker1 popup = ~htmlEscape(Name))
This returns marker in correct spot with popup displaying 'tufts'
Wondering if there is a way to encode a hyperlink into the popup directly in the CSV?Oor to put plain text as a new CSV column and have R/Shiny then turn it into a hyperlink.
Very new to shiny/leaflet and would appreciate any help!
Just include the link in the popup as html:
output$mymap <- renderLeaflet({
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup= ' R ')
m # Print the map
})
You can set the popup equal to a column in your dataframe as well. If your dataframe was called df and it contained longitude = long, latitude= lat, and urls = link :
output$mymap <- renderLeaflet({
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=df$long, lat=df$lat, popup= df$link)
m # Print the map
})
If you want the link to be a hyperlink, I have used this
popup = ~paste("", "click here", "")
provided your data has a column called link where the urls are.

Getting finer control of leaflet popups in r

I am trying to get finer control of leaflet popups in R, using the leaflet package. The code for a MWE is below:
library(dplyr)
library(magrittr)
library(leaflet)
download.file(
url = "http://biogeo.ucdavis.edu/data/gadm2.8/rds/GBR_adm1.rds",
destfile = "GBR_adm1.rds",
method = "curl"
)
shp_gbr <- readRDS("GBR_adm1.rds")
# get centroids for placing popups in second map
shp_gbr_centers <-
rgeos::gCentroid(shp_gbr, byid = TRUE) %>%
sp::SpatialPointsDataFrame(shp_gbr#data, match.ID = FALSE)
shp_gbr#data %<>%
left_join(shp_gbr_centers[1], by = 'OBJECTID', copy = TRUE) %>%
rename(lat = y, lng = x) %>%
select(NAME_1, lat, lng) %>%
mutate(text = ProgGUIinR::LoremIpsum)
popup <- paste("<b><h3>", shp_gbr$NAME_1, "</h3></b>", shp_gbr$text)
shp_gbr %>%
leaflet() %>%
addPolygons(popup = ~popup)
This gives a nice map with popups that appear on clicking within the areas of the 4 countries, but in this case, the text is too much for the popup to handle nicely:
What I would like is to access some of the popupOptions available via the addPopups function, in this case to make the popup wider and have a scroll bar. An example of this is below:
shp_gbr %>%
leaflet() %>%
addPolygons() %>%
addPopups(
data = shp_gbr#data,
popup = ~popup,
options =
popupOptions(
maxWidth = 600,
maxHeight = 100
)
)
However, the popups are now set to be open on launch, rather than appearing on clicking within the boundaries, and do not reopen on click once closed:
My question is how to combine these elements so that you could have, say, a scroll bar for too much text within a map such as the first example, where the popups are closed by default but open on click.
You could use this function to create your popup:
popup <- paste("<div class='leaflet-popup-scrolled' style='max-width:600px;max-height:100px'><b><h3>", shp_gbr$NAME_1, "</h3></b>", shp_gbr$text,"</div>")
It wraps the popup in a div with the leaflet-popup-scrolled class to add the scroll bar and inline CSS to set the max-width and max-height.

Resources