I'm currently working on pulling data from a PostGIS database for the first time to map in R and need some help. The database contains a shapefile of countries/regions of the world. I am able to pull the attributes into R as a table through the
dbReadTable function from the package
RPostgreSQL. This is great for manipulating the data (adding, merging, subsetting, etc.), but ultimately, I still need to be able to actually map this bad boy, so I need the data to be spatial in some way (side note: I plan on mapping with
ggplot2, so my data needs to be suitable for that). Here's the tiny bit of code I've successfully used to pull in the data as a table/data frame:
#load in required libraries library(RPostgreSQL) #connect to database drv <- dbDriver("PostgreSQL") DB <- dbConnect(drv, dbname = "dbname", user = "user", host = "host", password = "password", port = "5432") #pull in shapefile data world.shp <- dbReadTable(DB, "ne_10m_admin_1_states_provinces")
Again, I have no problems connecting to the database. That said, I need to pull the data in as a shapefile rather than as a data frame. This question seems to be what I'm looking for, but the accepted/top solution confuses me; specifically, I don't know how to tackle this bit of code to pull in the shapefile from the database:
Similarly, I've found various solutions for Ubuntu, but only this one for Windows, which spits out errors for me.