Skip to content

Publishing services

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.

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)

token <- auth_code()  # <1>
set_arc_token(token) # <2>
  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  
#>    <dbl>     <dbl> <dbl>   <dbl> <chr> 
#>  1 0.114      1.44  1825    1825 Ashe  
#>  2 0.061      1.23  1827    1827 Alleg…
#>  3 0.143      1.63  1828    1828 Surry 
#>  4 0.07       2.97  1831    1831 Curri…
#>  5 0.153      2.21  1832    1832 North…
#>  6 0.097      1.67  1833    1833 Hertf…
#>  7 0.062      1.55  1834    1834 Camden
#>  8 0.091      1.28  1835    1835 Gates 
#>  9 0.118      1.42  1836    1836 Warren
#> 10 0.124      1.43  1837    1837 Stokes
#> # ℹ 90 more rows
#> # ℹ 10 more variables: FIPS <chr>,
#> #   FIPSNO <dbl>, CRESS_ID <int>,
#> #   BIR74 <dbl>, SID74 <dbl>,
#> #   NWBIR74 <dbl>, 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

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:

published items

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:

nc sids

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")

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.