Blank popup in leaflet R - r

I am trying to map San Francisco's crimes in a map. The below code intends to map every crime (lat, lng) and when the marker is clicked, show the "category" column of the dataset.
Right now the below code shows a blank text box when I click the marker.
Can anybody help?
sf <- read.csv("https://raw.githubusercontent.com/uwescience/datasci_course_materials/master/assignment6/sanfrancisco_incidents_summer_2014.csv")
crime <- data.frame(lat = c(sf$Y),
lng = c(sf$X))
cat <- c(sf$Category)
library(leaflet)
crime %>%
leaflet() %>%
addTiles() %>%
addMarkers(popup = paste(sf$Category), clusterOptions = markerClusterOptions())

Try the following:
sf <- read.csv("https://raw.githubusercontent.com/uwescience/datasci_course_materials/master/assignment6/sanfrancisco_incidents_summer_2014.csv")
library(leaflet)
sf %>%
leaflet() %>%
addTiles() %>%
addMarkers(lat = ~Y, lng = ~X, popup = ~Category, clusterOptions = markerClusterOptions())
I'm not sure what your issue is, but using the formula syntax allows leaflet to build the list of pop-up labels on its own and does not require calling paste explicitly or subsetting the original dataframe.

Related

How do I get leaflet marker labels in R to show HTML AND include label for only location of marker

I am showing multiline labels in leaflet markers. Each label combines several columns of data. If if I just paste the columns together, I get one location per marker but no HTML rendering (Example 1). If I wrap the label with HTML() I get proper rendering but EVERY location is in each marker (Example 2). How do I get the desired content AND the desired formatting? Thanks.
library(htmltools)
library(leaflet)
library(dplyr)
df <- read.csv(textConnection(
"Name,Rating,Lat,Long
Samurai Noodle,Good, 47.597131,-122.327298
Kukai Ramen,Fair,47.6154,-122.327157
Tsukushinbo,Great,47.59987,-122.326726"))
# EXAMPLE 1. One location per marker but HTML not rendered
leaflet(df) %>%
addTiles() %>%
addMarkers(~Long, ~Lat,
label = ~ paste(Name,Rating,sep = "<br/>"))
# EXAMPLE 2. HTML rendered but all locations in each marker
leaflet(df) %>% addTiles() %>%
addMarkers(~Long, ~Lat,
label = ~ HTML(paste(Name,Rating,sep = "<br/>")))
Maybe there is an easier option. But one fix for your issue would be to use lapply to apply HTML on each element of your vector of labels which avoids that your labels are glued together:
library(htmltools)
library(leaflet)
library(dplyr)
df <- read.csv(textConnection(
"Name,Rating,Lat,Long
Samurai Noodle,Good, 47.597131,-122.327298
Kukai Ramen,Fair,47.6154,-122.327157
Tsukushinbo,Great,47.59987,-122.326726"
))
leaflet(df) %>%
addTiles() %>%
addMarkers(~Long, ~Lat,
label = ~lapply(paste(Name, Rating, sep = "<br>"), HTML)
)
I had a similar problem in my own app that was solved by creating a column in the data frame before generating the map that includes the desired formatting. Also, for reasons I don't understand, you then pass that label to popup rather than label in the call to addMarkers(). So...
df$marker <- with(df, sprintf("%s </br> %s", Name, Rating))
leaflet(df) %>%
addTiles() %>%
addMarkers(~Long, ~Lat,
popup = ~marker)
...gives what I think is the result you're looking for.

Trouble using the leaflet package to make an interactive map using the us.cities package

I am trying to make an interactive graph of the United States using leaflet and the us.cities package in R. I was able to create the map and label put markers on all the US capitals, but I am struggling to label those capitals on the map itself.
The goal is to be able to click on a marker and have the state capital and population appear on the graph. The trouble is steaming from my last section of code that starts with the #. If anyone has any idea why that code will not run, besides the obvious fact that I put a # in front, I am open to any and all guidance!
Thank you!
library(leaflet)
library(dplyr)
library(maps)
leaflet(us.cities) %>% addTiles() %>% addCircleMarkers(data = us.cities)
us.cities <- us.cities %>% filter(capital == 2) %>% mutate(state_info = paste(name, capital == 2, pop))
us.cities$state_info
#leaflet(us.cities) %>% addTiles() %>% addCircleMarkers(data = us.cities, lat = ~lat, lng = ~long, state_info = ~state_info)
I don't know much about the leaflet package, but for the purposes of providing a quick answer...
leaflet(us.cities) %>%
addTiles() %>%
addCircleMarkers(
data = us.cities,
lat = ~lat,
lng = ~long,
popup = paste(us.cities$country.etc, us.cities$pop)
)

Creating a heatmap on R using leaflet function add_heatmap() or addHeatmap()

I am trying to create a plot with heatmap using plotly add_heatmap() function, but it generates an error message saying Error: Must supply z attribute I referred to this site for addHeatmap() function provided by leaflet.extras package.
The following code only displays dots from addCircles() and outputs a warning message and a map as shown below:
final_df %>%
leaflet() %>%
addTiles() %>%
addCircles(
lng = final_df$long,
lat = final_df$lat,
popup = final_df$station_name
) %>% addHeatmap(lng = final_df$long, lat = final_df$lat, radius=5)
I have tried a different function add_heatmap() from leaflet which does not display any maps and generates an error.
final_df %>%
leaflet() %>%
addTiles() %>%
addCircles(
lng = final_df$long,
lat = final_df$lat,
popup = final_df$station_name
) %>% add_heatmap(lng = final_df$long, lat = final_df$lat)
Anyone faced a similar issue and created a heatmap on R?
Remove addCircles part and try:
library(leaflet.extras)
final_df %>%
leaflet() %>%
addTiles() %>%
addHeatmap(lng = final_df$long, lat = final_df$lat, blur = 40, max = 0.05, radius = 15)

How to resize large images in R Leaflet marker popups?

My code:
library(leaflet)
df <- as.data.frame(read.csv("arts.csv"))
file <- as.character(df$url)
leaflet() %>% addTiles()
%>% addMarkers(data = df, lng = ~lon, lat = ~lat,
popup = paste0("<img src = ", file, ">"))
%>% popupOptions(maxWidth = "auto")
I used some code snippet from here: Image in R Leaflet marker popups
My question is: how can I easily resize large images appearing in popups?
The illustartion of the problem:
For Example, I would like to make this photo of Cave Rapa Nui smaller to fit the pop-up.
Thank you in advance!
You can adjust the size by using the HTML width attribute.
Edit your code from above to e.g.
map <- leaflet() %>%
addTiles() %>%
addMarkers(data = df,
lng = ~lon,
lat = ~lat,
popup = paste0("<img src = ", file, " width = 300>"))
map # print the map

R leaflet: adding polygons from shapefile removes tiles

I have a shapefile of neighborhood areas in NYC (https://nycopendata.socrata.com/City-Government/Neighborhood-Tabulation-Areas/cpf4-rkhq).
When I use use leaflet to overlay these polygons the background tiles are not shown.
This is the code I am using:
library(rgdal)
library(leaflet)
nyc = readOGR("geo_export_da30afd0-e475-44e2-90d2-aca31344ef5e.shp",
layer="geo_export_da30afd0-e475-44e2-90d2-aca31344ef5e")
m = leaflet() %>% fitBounds(lng1 = -74.458921, lat1 = 40.550302,
lng2 = -73.683035, lat2=40.89225)
m %>% addTiles()
m %>% addPolygons(data=nyc)
This is the output:
m %>% addTiles() %>% addPolygons(data = nyc)
should work. %>% does not assign but only pipes contents, therefore needs to be a complete chain.

Resources