Publishing from R

Create Hosted ArcGIS Online or Enterprise Feature Services from R

In addition to consuming data as an R user, you may also want to publish data as a hosted feature service. In this tutorial, you will learn how to publish an sf object to ArcGIS Online or Enterprise.

Authorization

In order to publish content to ArcGIS Online or Enterprise, you must first obtain an access token permitting you to do so.

Caution

If you have not yet set up your R environment for authorization, see Authorize with your Portal. Ensure that the environment variables ARCGIS_CLIENT and ARCGIS_USER are set at minimum. If you are using ArcGIS Enterprise, ensure that ARCGIS_HOST is properly set as well.

Go through the following code flow to set your credentials.

library(arcgis)

1token <- auth_code()
2set_arc_token(token)
1
Create an access token
2
Set it to an environment variable.

Now that you have authorized to your Portal, you will be able to publish content.

Publishing {sf} objects

To publish an {sf} object to your portal, you can use the function publish_layer(). The publishing process requires you to add an item to your portal and publish it. The publish_layer() function handles these steps for you.

First, read in the North Carolina SIDS dataset that comes packaged with sf and store it in an object called nc.

nc <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))
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 you have an sf object and you have authorized with your 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 you are creating
res <- publish_layer(nc, "North Carolina SIDS")
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
Warning

If you encounter errors while publishing, try using a feature layer title that does not contain spaces or special characters, such as “NorthCarolinaSIDS” for this example.

Now from your Portal’s Content page you should see two items associated with your feature service:

Behind the scenes, publish_layer() added the sf object as a Feature Layer item first and then published this item as a Feature Layer (hosted). After publishing, you will typically only interact with the hosted feature layer. (Note that the dependency between these two items prevents you from deleting the underlying feature layer while the hosted feature layer still exists.)

Click View details on the hosted feature layer item and you should see something like the below:

Reading the published Feature Layer

The output of the publish_layer() function is a list that contains information about where the sf object was published. You can retrieve the encodedServiceUrl from the response and use arc_open() to return the metadata for your newly-created service.

nc_fserver <- arc_open(res[[c("services", "encodedServiceURL")]])
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.

You can extract a single layer from the FeatureServer using get_layer(). Provide the FeatureServer as the first argument and then the ID of the layer you 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

Publishing data.frames

Publishing 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")
palmerpenguins::penguins
publish_layer(palmerpenguins::penguins, "Palmer Penguins")