rr-sfr-mapviewr-mapedit

Converting a data frame to a spatial object


I'm trying to publish a map on Shiny, but keep running into this issue.

Let's call the data.frame region. Here are the columns:

  library(mapedit)
  library(mapview)
  library(shiny)
  library(leaflet)
  library(leaflet.extras)   



  region$address
  region$city
  region$state
  region$zip
  region$county
  region$xcol (these are the longitude coordinates)
  region$ycol (these are the latitude coordinates)

But when I run the mapview(region)@map it produces the following error:

Error: oops! Arguments xcol and/or ycol are missing! You probably expected turf_clean to be a spatial object. However it is of class data.frame. Either convert turf_clean to a spatial object or provide xcol and ycol.

I provided the x and y cols, but it's still not producing what I need.


Solution

  • The library sf and its st_as_sf() is a way to convert your data.frame to a spatial object. crs refers to the datum and projection, which I just guessed assuming your lat/long are relative to the WGS84 datum. I believe the projection is what maps.google.com uses - web mercator auxiliary sphere.

    library(sf)
    turf_clean <- st_as_sf(region, coords = c("xcol", "ycol"), crs = 4326)