Skip to content

Commit

Permalink
Merge pull request #14 from R-ArcGIS/updatepublishing
Browse files Browse the repository at this point in the history
Update set_arc_token and phrasing
  • Loading branch information
JosiahParry authored Feb 28, 2024
2 parents 577aed4 + fdccb09 commit 0661dbb
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions location-services/publishing.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@ subtitle: "Create Hosted ArcGIS Online or Enterprise Feature Services from R"
freeze: true
---

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 <!-- or a `data.frame`--> to ArcGIS Online or Enterprise.
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 <!-- or a `data.frame`--> to ArcGIS Online or Enterprise.

## Authorization

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

:::{.callout-caution}
If you have not yet set up your R environment for authorization, see [**Connecting to your Portal**](./connecting-to-a-portal.qmd). Ensure that the environment variables `ARCGIS_CLIENT` and `ARCGIS_USER` are set at minimum. If you are using Enterprise ensure that `ARCGIS_HOST` is properly set as well.
If you have not yet set up your R environment for authorization, see [**Authorize with your Portal**](./connecting-to-a-portal.qmd). 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.
:::

We must go through the code flow to set our credentials.
Go through the following code flow to set your credentials.

```{r, eval = FALSE}
library(arcgis)
token <- auth_code() # <1>
set_auth_token(token) # <2>
set_arc_token(token) # <2>
#> Token set to environment variable `ARCGIS_TOKEN`
```
1. We create an access token
2. We set it to an environment variable.
1. Create an access token
2. Set it to an environment variable.

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

## Publishing {sf} objects

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.
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.

Let's read in the [North Carolina SIDS](https://cran.r-project.org/web/packages/spdep/vignettes/sids.html) dataset that comes packaged with `sf` and store it in an object called `nc`.
First, read in the [North Carolina SIDS](https://cran.r-project.org/web/packages/spdep/vignettes/sids.html) dataset that comes packaged with `sf` and store it in an object called `nc`.

```{r}
nc <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))
nc
```

Now that we have an sf object and we have authorized with our portal, all that's left is to publish the item!
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 we are creating
- `title` the title of layer you are creating

```{r, eval=FALSE}
res <- publish_layer(nc, "North Carolina SIDS")
Expand All @@ -60,14 +60,18 @@ res
#> 1 https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/North%20Carolina%20SIDS/FeatureServer
```

:::{.callout-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 listing](https://arcgis.com/home/content.html) you should see your feature service. If you open it up you should see something like the below.

![](images/publish/nc-sids.png){width=70%}

## Reading the published Feature Layer

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.
You can retrieve the `encodedServiceUrl` from the response and read the response.

```{r eval=FALSE}
nc_fserver <- arc_open(res[[c("services", "encodedServiceURL")]])
Expand All @@ -80,7 +84,7 @@ nc_fserver

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.
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.

```{r eval=FALSE}
get_layer(nc_fserver, 0)
Expand Down

0 comments on commit 0661dbb

Please sign in to comment.