There and Back Again competition entry https://shiny.petras.space/commute/
rstats
rshiny
census
competition
leaflet
javascript
stats-nz

app.R 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. library(shiny)
  2. library(leaflet)
  3. library(rgdal)
  4. library(readr)
  5. library(dplyr)
  6. work_travel <- read_csv("../travel-work.csv")
  7. shpf <- readOGR(dsn="../shapefiles/sa20025WGSfilcth")
  8. # Define UI
  9. ui <- fluidPage(
  10. leafletOutput("map")
  11. )
  12. # Define server logic
  13. server <- function(input, output) {
  14. output$map <- renderLeaflet({
  15. leaflet(shpf, options = leafletOptions(minZoom = 3, maxZoom = 13)) %>%
  16. addPolygons(color="#000", opacity = 1, weight=1,
  17. popup = shpf@data$SA22018__1) %>%
  18. setView(174, -41, 5)
  19. })
  20. observeEvent(input$map_shape_click, {
  21. p <- input$map_shape_click
  22. print(p)
  23. pdat <- data.frame(Longitude = p$lng,
  24. Latitude =p$lat)
  25. # Assignment modified according
  26. coordinates(pdat) <- ~ Longitude + Latitude
  27. # Set the projection of the SpatialPointsDataFrame using the projection of the shapefile
  28. proj4string(pdat) <- proj4string(shpf)
  29. ppoly <- over(pdat, shpf)
  30. print(ppoly)
  31. print(work_travel[work_travel$SA2_code_usual_residence_address ==
  32. ppoly[1,"SA22018_V1"],])
  33. })
  34. }
  35. # Run the application
  36. shinyApp(ui = ui, server = server)