I have been working with Leaflet for creating cool interactive maps in R. However, I cannot export the maps, as the background map ends up being grey after export.
library(leaflet)
library(htmlwidgets)
m <- leaflet(data.frame(lat = 55.71654, lng = 12.47484))
m <- addCircles(m, popup = "testpopup")
m <- addTiles(m)
m
saveWidget(m, file="testmap.html", selfcontained = TRUE)
Is there an alternative to Leaflet where you can export your interactive maps as HTML? Or do I have to get into the whole Leaflet/Shiny thing?
As mentioned my #Martin Schmelzer then it all works after updating leaflet through the devtools::install_github('rstudio/leaflet'). Thanks.
Related
I have used the following code to produce a map on R. The issue is when I try and export as a HTML I get this error:
popup.css not found in resource path
When I export as a local html it works fine. Does anyone know why this is and how to fix it? Any help would be greatly appreciated!
#Load shape file
UK_Map<-gadm36_GBR_2_sp
plot(UK_Map)
#Retail Data and Postcodes
Benchmark_Retail <- read_excel("C:/Users/andrew.smith2/Desktop/Benchmark output_v3 (1920).xlsx")
Postcodes <- read_excel("C:/Users/andrew.smith2/Desktop/Postcodes.xlsx")
#Merge Datasets
Retail_data<-merge(Benchmark_Retail,Postcodes,by="Postcode")
Retail_data2<-as.tibble(Retail_data)
locations_sf <- st_as_sf(Retail_data2, coords = c("longitude", "latitude"), crs = 4326)
m=mapview(locations_sf)
mapview(locations_sf)
mapshot(m, url = paste0(getwd(), "/map.html"))
I need to create a web page that includes an interactive map where users can see popup information about data collected at many locations. Using Rstudio and leaflet on Windows, wanting to use OSM base map tiles.
My leaflet map works fine in Rstudio viewer. However, when the 'knitted' page is viewed in Firefox, no OSM map tiles appear although other components of the map are okay. Similarly OSM tiles missing in saved html widget.
I made a simple example to demonstrate.
```{r}
library(leaflet)
library(htmlwidgets)
rand_lng = function(n = 10) rnorm(n, 145.7, .01)
rand_lat = function(n = 10) rnorm(n, -17, .01)
m = leaflet() %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles("Esri.WorldImagery") %>%
addCircleMarkers(rand_lng(5), rand_lat(5), group = "Points")%>%
addLayersControl(
baseGroups = c("OSM (default)","Esri.WorldImagery"),
overlayGroups = c("Points"),
options = layersControlOptions(collapsed = FALSE)) %>%
setView(lng = 145.7, lat = -17, zoom = 12)
m
saveWidget(m, "leaflet_OSMplusEsri.html")
```
This is the output I get in Rstudio viewer, with OSM tiles selected and displayed correctly. When selected, Esri tiles are correct also.
This is the html file shown in Firefox, where OSM tiles do not display despite being selected.
I've been searching all day without discovering how to troubleshoot this. As a newbie perhaps I'm missing something obvious?
I will be very grateful for advice: how to troubleshoot this problem in simple steps?
Took a long time, but I eventually resolved this. In case it helps anyone else, here is the revised version that works properly.
```{r}
library(leaflet)
library(htmlwidgets)
rand_lng = function(n = 10) rnorm(n, 145.7, .01)
rand_lat = function(n = 10) rnorm(n, -17, .01)
m = leaflet() %>%
addProviderTiles(providers$OpenStreetMap, group = "OSM") %>%
addProviderTiles(providers$Esri.WorldImagery, group = "Esri") %>%
addCircleMarkers(rand_lng(5), rand_lat(5), group = "Points")%>%
addMiniMap() %>%
addLayersControl(
baseGroups = c("OSM","Stamen", "Esri"),
overlayGroups = c("Points"),
options = layersControlOptions(collapsed = FALSE)) %>%
setView(lng = 145.7, lat = -17, zoom = 12)
m
saveWidget(m, "leaflet_OSMplusEsri.html")
```
Cause of the problem was addTiles() with default values. I'm not sure why this did not work, hoping someone might be able to explain.
I would like to intergrate a couple of polygons into my leaflet map. These polygons are written in WKT. By using the package wellknown i convert them to a geosjon format
InstallCandidates <-c("jsonlite","leaflet","wellknown","rgdal","sp")
toInstall<-InstallCandidates[!InstallCandidates %in% library()$results[,1]]
if(length(toInstall) !=0){install.packages(toInstall,repos="http://cran.r-project.org")}
lapply(InstallCandidates,library,character.only=TRUE)
rm("InstallCandidates","toInstall")
url<- "https://kaart.amsterdam.nl/datasets/datasets-item/t/cora-geplande-wegwerkzaamheden/export/json"
werkzaamheden <- as.data.frame(fromJSON(url))
The var linestring is one WKT object in this url.
linestring<- "LINESTRING (4.9473233812748 52.304767613036, 4.9473234970894 52.304755910603, 4.947323612904 52.30474420817, 4.9473350439563 52.304746591253, 4.9473310174218 52.304767641433, 4.9473119270542 52.304767570438)"
locatie<- wkt2geojson(linestring, feature = FALSE)
class(locatie)
Is tried to plot this linestring in a leaflet object by doing this:
leaflet(data = locatie) %>%
addTiles() %>%
addGeoJSON(geojson =locatie, color = "red",fill = "red")
But i dont see the linestring, could anyone help me pleas?
Thanks
I have a script which allows me to generate a map with with "R for leaflet" :
library(htmlwidgets)
library(raster)
library(leaflet)
# PATHS TO INPUT / OUTPUT FILES
projectPath = "path"
#imgPath = paste(projectPath,"data/cea.tif", sep = "")
#imgPath = paste(projectPath,"data/o41078a1.tif", sep = "") # bigger than standard max size (15431804 bytes is greater than maximum 4194304 bytes)
imgPath = paste(projectPath,"/test.tif", sep = "")
outPath = paste(projectPath, "/leaflethtmlgen.html", sep="")
# load raster image file
r <- raster(imgPath)
# reproject the image, if necessary
#crs(r) <- sp::CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
# color palette, which is interpolated ?
pal <- colorNumeric(c("#FF0000", "#666666", "#FFFFFF"), values(r),
na.color = "transparent")
# create the leaflet widget
m <- leaflet() %>%
addTiles() %>%
addRasterImage(r, colors=pal, opacity = 0.9, maxBytes = 123123123) %>%
addLegend(pal = pal, values = values(r), title = "Test")
# save the generated widget to html
# contains the leaflet widget AND the image.
saveWidget(m, file = outPath, selfcontained = FALSE, libdir = 'leafletwidget_libs')
My problem is that this is generating a html file and I need this map to be dyanamic. For example, when a user click on some html button which is not integrate on the map, I want to add a rectangle on the map. Any solutions would be welcome...
Leaflet itself does not provide the interactive functionality you are looking for. One solution is to use shiny, which is a web application framework for R. From simple R code, it generates a web page, and runs R on the server-side to respond to user interaction. It is well documented, has a gallery of examples, and a tutorial to get new users started.
It works well with leaflet. One of the examples on the shiny web site uses it, and also includes a link to the source code.
Update
Actually, if simple showing/hiding of elements is enough, leaflet alone will suffice with the use of groups. From the question it's not very clear how dynamic you need it to be.
I am using RStudio to create some some leaflet images.
I would like to be able to save the output as an HTML so that it can be emailed and others can view it.
Below is some sample R code which was taken from [here] to create a sample leaflet image.
devtools::install_github('rstudio/leaflet')
library(leaflet)
rand_lng = function(n = 10) rnorm(n, -93.65, .01)
rand_lat = function(n = 10) rnorm(n, 42.0285, .01)
m = leaflet() %>% addTiles() %>% addCircles(rand_lng(50), rand_lat(50), radius = runif(50, 10, 200))
m
Any code to be able to the output as HTML would be much appreciated...
Something like:
library(htmlwidgets)
saveWidget(m, file="m.html")
seems to work on most widgets.
Open a new RMarkdown document. When you are using RStudio go to File -> New File -> R Markdown.
Once you saved the file, you can insert your code into a chunk, like this:
---
title: "Leaflet Map"
output: html_document
---
```{r}
library(leaflet)
rand_lng = function(n = 10) rnorm(n, -93.65, .01)
rand_lat = function(n = 10) rnorm(n, 42.0285, .01)
m = leaflet() %>% addTiles() %>% addCircles(rand_lng(50), rand_lat(50), radius = runif(50, 10, 200))
m
```
Then Press the Knit HTML Button above the code window and your application will open in a new HTML file. You can send the file via eMail or upload it to your ftp.
I have faced the same problem and after installing Github version the problem was fixed.
# Or Github version
if (!require('devtools')) install.packages('devtools')
devtools::install_github('rstudio/leaflet')
My present version is 1.1.0.9000, running on macOS Sierra, RStudio Version 1.1.232 and R 3.4.0
You can export from RStudio or save using htmlwidgets.
Another option using mapview library is:
library(mapview)
mapshot(m, url = "m.html")
Note that you can also set the output to .png, .pdf, or .jpeg.
library(mapview)
To save as a "png" or "jpg" image:
mapshot(m, file = "m.png")
mapshot(m, file = "m.jpeg")
Even pdf can be used
Both solutions saveWidget or mapshot work correctly (saveWidget seems to be quicker), however, you should take care with color selection, especially in those choosed for borders/lines of polygons because in the stored map not all colours in borders are drawn ("grey50" for example is ignored while pure colours as "black" are drawn normally).
Curiously, these colours are stored and shown correctly when they are used as fill colour.