Leaflet map within a shiny HTML layout

Hi,

I want to incorporate a leaflet map within a shiny web page layout. I'm new to 'RStudio' but have managed to create a leaflet map which queries a table in postgreSQL/PostGIS as below.

I need to place this code inside a shiny html page so I can apply a title (no other widgets required yet).

Get RPostgreSQL and some other GIS packages

install.packages("RPostgreSQL")
install.packages("rpostgis")
install.packages("sp")
install.packages("rgdal")
install.packages("leaflet")
install.packages("leaflet.extras")

Open the library sessions

library(RPostgreSQL)
library(rpostgis)
library(sp)
library(rgdal)
library(leaflet)
library(leaflet.extras)

Specify Driver

pg <- dbDriver("PostgreSQL")

PostGIS Connection Details

con <- dbConnect(pg, user ='xxxx', password ="xxxx", host = "xxxx" , port = 5432, dbname="xxxx")

List all Tables in PostGIS database to check we can retrieve all available

dbListTables(con)

Query to PostGIS table

#-----------------------------
query <- "SELECT * FROM table_x WHERE type = 'es'"
es <- pgGetGeom(con, c("schema","table_x"), geom = "wkb_geometry", gid = "ogc_fid", other.cols = FALSE)

Plot the layer in Viewer

plot(es,axes=TRUE)

Change the projection from OSGB36 (epsg:27700) to WGS 84 (epsg:4326)

es <- spTransform(es, CRS("+init=epsg:4326"))

Add a Leaflet Basemap

#-----------------------------
cls<-terrain.colors(100)[100:1]
library(leaflet)
map<-leaflet(es) %>%
setView(-3.4710, 51.8633, zoom = 10) %>%
addTiles(group="osm") %>%
addProviderTiles("OpenTopoMap") %>%
addPolygons(data=es,group="mygroup") %>%
addFullscreenControl() %>%
addLayersControl(baseGroups = c("osm","Satellite"),
overlayGroups = c("esgroup"),
options = layersControlOptions(collapsed = TRUE))
map

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.