library(arcgis)
1<- auth_code()
token 2set_auth_token(token)
#> Token set to environment variable `ARCGIS_TOKEN`
- 1
- We create an access token
- 2
- We set it to an environment variable.
Create Hosted ArcGIS Online or Enterprise Feature Services from R
While you may often consume data as an R user, you may also want to also publish data as a hosted feature service. In this tutorial we will go over how to publish an sf
object to ArcGIS Online or Enterprise.
To publish an {sf}
object to our portal, we can use the function publish_layer()
. The publishing process requires us to add an item to our portal and publish it. The publish_layer()
function handles these steps for us.
Let’s read in the North Carolina SIDS dataset that comes packaged with sf
and store it in an object called nc
.
<- sf::read_sf(system.file("shape/nc.shp", package = "sf"))
nc nc
Simple feature collection with 100 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS: NAD27
# A tibble: 100 × 15
AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74
<dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <int> <dbl> <dbl> <dbl>
1 0.114 1.44 1825 1825 Ashe 37009 37009 5 1091 1 10
2 0.061 1.23 1827 1827 Alle… 37005 37005 3 487 0 10
3 0.143 1.63 1828 1828 Surry 37171 37171 86 3188 5 208
4 0.07 2.97 1831 1831 Curr… 37053 37053 27 508 1 123
5 0.153 2.21 1832 1832 Nort… 37131 37131 66 1421 9 1066
6 0.097 1.67 1833 1833 Hert… 37091 37091 46 1452 7 954
7 0.062 1.55 1834 1834 Camd… 37029 37029 15 286 0 115
8 0.091 1.28 1835 1835 Gates 37073 37073 37 420 0 254
9 0.118 1.42 1836 1836 Warr… 37185 37185 93 968 4 748
10 0.124 1.43 1837 1837 Stok… 37169 37169 85 1612 1 160
# ℹ 90 more rows
# ℹ 4 more variables: BIR79 <dbl>, SID79 <dbl>, NWBIR79 <dbl>,
# geometry <MULTIPOLYGON [°]>
Now that we have an sf object and we have authorized with our portal, all that’s left is to publish the item!
publish_layer()
has only two required arguments:
x
the sf object or data.frame
title
the title of layer we are creating<- publish_layer(nc, "North Carolina SIDS")
res
res#> $services
#> type
#> 1 Feature Service
#> serviceurl
#> 1 https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North Carolina SIDS/FeatureServer
#> size jobId serviceItemId
#> 1 125766 f14451a7-325b-40b0-85c3-534bcf122806 32511ce0413f40d08303e267a7093be0
#> encodedServiceURL
#> 1 https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North%20Carolina%20SIDS/FeatureServer
Now from your Portal’s Content listing you should see your feature service. If you open it up you should see something like the below.
The output of this function is a list that contains information about where the sf object was published. We can retrieve the encodedServiceUrl
from the response and read the response.
<- arc_open(res[[c("services", "encodedServiceURL")]])
nc_fserver
nc_fserver#> <FeatureServer <1 layer, 0 tables>>
#> CRS: 4267
#> Capabilities: Create,Delete,Query,Update,Editing
#> 0: North Carolina SIDS (esriGeometryPolygon)
You’ll notice that this is a FeatureServer
. All items that are published to a Portal become their own Feature Server with a single FeatureLayer
.
We can extract a single layer from the FeatureServer
using get_layer()
. We provide the FeatureServer
as the first argument and then the ID of the layer we want as the second argument.
get_layer(nc_fserver, 0)
#> <FeatureLayer>
#> Name: North Carolina SIDS
#> Geometry Type: esriGeometryPolygon
#> CRS: 4267
#> Capabilities: Create,Delete,Query,Update,Editing
data.frame
sPublishing a data.frame
follows the same steps as those above. The difference is that it creates a Table
object. Try repeating the same process but using the palmerpenguins dataset!
# install.packages("palmerpenguins")
::penguins palmerpenguins
# A tibble: 344 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 39.5 17.4 186 3800
3 Adelie Torgersen 40.3 18 195 3250
4 Adelie Torgersen NA NA NA NA
5 Adelie Torgersen 36.7 19.3 193 3450
6 Adelie Torgersen 39.3 20.6 190 3650
7 Adelie Torgersen 38.9 17.8 181 3625
8 Adelie Torgersen 39.2 19.6 195 4675
9 Adelie Torgersen 34.1 18.1 193 3475
10 Adelie Torgersen 42 20.2 190 4250
# ℹ 334 more rows
# ℹ 2 more variables: sex <fct>, year <int>
publish_layer(palmerpenguins::penguins, "Palmer Penguins")