The making of an R API to interface with ArcGIS Online and ArcGIS Enterprise.
- Why not? We should have some sort of API for every serious data science language. That includes R.
- DataFrames. In R, everything is a dataframe. It makes data exploration very intuitive.
- There's already a ton there.
- Download and install the package from GitHub:
devtools::install_github("EsriPS/aRcgis",ref="main", auth_token = "yourpersonalauthtoken")
- Import package package:
library(arcgis)
- Import the R
sp
package:library(sp)
- Create a GIS Onbject from your portal URL, username, and password. This object will keep your credentials and session token.
gis <- new("GIS",
url="https://learngis.maps.arcgis.com/",
username = "gbrunner_LearnGIS",
password = "Password")
- Log into ArcGIS Online
gis <- login(gis)
print(gis)
- Define a serch term, for example:
search_term <- "owner:gbrunner_LearnGIS AND NJ AND type:feature service"
- Use the
search_gis
function to search for items:items <- search_gis(gis, search_term)
- Print out the results:
print(head(items$results))
- From the
items
dataframe, pass the first result to theget_service
function:service <- get_service(gis,items$results[1,])
- Print out the results:
print(service)
- Use
get_sdf
to turn the feature layer into a spatial dataframes:pdf <- get_sdf(gis, items$results[1,], 0)
- Print out the results:
print(head(spdf))
- Plot the polygons using
plot(spdf)
. - Plot the polygons symbolized by the "TOTPOP_CY" attribute using
spplot(spdf, 'TOTPOP_CY')
- Import the leaflet library:
library(leaflet)
. - Create a map and add the layer:
pal <- colorNumeric("viridis", NULL)
leaflet(spdf) %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
fillColor = ~pal(log10(TOTPOP_CY)),
label = ~paste0(TOTPOP_CY, ": ", formatC(TOTPOP_CY, big.mark = ",")))