Reputation: 449
I have a UK postcode for which I have latitude and longitude values, lets call this the origin postcode. I have a data frame which contains other postcodes and their corresponding latitude and longitude. I have a sliding bar input which is the radius from the origin postcode in miles, with this having a range up to 200 miles. I can load the app, but I can't see the other postcode markers appear on the map. I also get the below
Discarded datum OSGB_1936 in CRS definition Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO") : Discarded datum OSGB_1936 in CRS definition
I post my code below:
library(shiny)
library(leaflet)
library(shinyjs)
library(rgeos)
library(tidyverse)
crime_df <-
data.frame(Postcode =
"WN1 3LU",box =2,
Latitude =
53.546367,
Longitude =
-2.620909)
crime <-
data.frame(Postcode =
c("BL7 9YH","BT36 7WE"),box =c(7,1),
Latitude =
c(53.613982,53.613982),
Longitude =
-2.406439,-2.406439)
coordinates(crime) = ~Longitude+Latitude
proj4string(crime) = CRS("+init=epsg:4326")
crime <- spTransform(x = crime, CRS = CRS("+init=epsg:27700"))
ui <- fluidPage(shinyjs::useShinyjs(),
fluidPage(
# Give the page a title
titlePanel("Crime Map"),
mainPanel(leafletOutput("map")),
fluidRow(column(
3,
sliderInput(
"miles",
"Miles from location",
min = 1,
max = 200,
value = 100,
width = '120px'
)))))
server <- function (input, output, session) {
output$map <- renderLeaflet({
inside_df <- inside_df()
leaflet(crime_df) %>%
addTiles() %>%
setView(lng = -1.525,
lat = 55,
zoom = 5) %>%
addMarkers(
lng = inside_df$Longitude,
lat = inside_df$Latitude,
popup = inside_df$Postcode
)
})
circle <- reactive({
location <- crime_df %>%dplyr::select(Latitude, Longitude)
coordinates(location) <- ~Longitude+Latitude
proj4string(location) = CRS("+init=epsg:4326")
location <- spTransform(location, CRS = CRS("+init=epsg:27700"))
circle <- gBuffer(location, width = input$miles * 1609.34)
circle
})
inside_df <- reactive({
circle = circle()
inside = crime[circle,] # find points inside the circle
inside = spTransform(inside, CRS("+init=epsg:4326"))
inside_df = as.data.frame(inside)
inside_df
})
}
shinyApp(ui = ui, server = server)
Upvotes: 0
Views: 66
Reputation: 6567
Your data frame has both postcodes in the same location and contains an error. Changing it as below
crime <-
data.frame(
Postcode = c("BL7 9YH", "BT36 7WE"),
box = c(7, 1),
Latitude = c(53.613982, 54.66653),
Longitude = c(-2.406439,-5.981969)
)
yields this.
Upvotes: 0