Read a feature service with R
Install the metapackage for R
Before you can analyze DC's street trees, you'll need to connect to the online data. First, you'll install the {arcgis} metapackage, part of the R-ArcGIS Bridge.
The {arcgis} metapackage is a collection of R packages that integrate the R programming language and ArcGIS data and location services from ArcGIS Online, ArcGIS Enterprise, and ArcGIS Location Platform. Installing the metapackage pulls in the included packages for workflows like reading and writing feature layers ({arcgislayers}), authenticating to ArcGIS Online or ArcGIS Enterprise ({arcgisutils}), and geocoding ({arcgisgeocode}). You'll only need to install the {arcgis} metapackage once. Then, you can load it in future R sessions.
- Start Positron or another R-supported integrated development environment (IDE).
Note:
Positron is an IDE that is used for R programming. You can use another R-supported IDE if you choose, but some steps may be different in another IDE.
- On the Welcome page, click New Folder.

- In the Folder Template window, click R Project.

- Click Next.
- In the New Folder From Template window, for Folder Name, type my-r-project.
- For Location, click Browse. Browse to a folder for your project, such as C:\Project.
Note:
If you don't have a C:\Project folder, you can create one.

- Click Next.
- In the Project Configuration window, confirm that a valid version of R is selected

Note:
If you do not see a version of R listed, you may not have R installed on your machine. If so, close Positron and download and install an R version between 4.3 and 4.5. If you have multiple versions of R, make sure to choose one between 4.3 and 4.5.
- Click the Create button.
- In the New Folder Created window, click Current Window.

- In the Explorer pane, click the New File button.

- Name the file 01-read-a-feature-service.R.

It's important to include the .R file extension to define the file as an R script. Next, you'll download the {arcgis} metapackage, which allows you to use ArcGIS functionality in R.
- Copy and paste the following code into the script window:
install.packages(c("arcgis", "sf", "dplyr", "mapgl")) - Click the line number 1 to select the line of code.

- Press Ctrl+Enter to run the selected line of code.
The code runs and the Console pane shows the results.

The packages are downloaded and installed.
Note:
If you see a message that the packages cannot be installed at the current location, accept the option to install the packages locally in your home directory.
- Add a line to the script. Copy and paste the following code into the new line:
library(arcgis) # loads arcgislayers, arcgisutils, arcgisgeocode library(sf) # for handling spatial data frames library(dplyr) # for data manipulation library(mapgl) # for plotting - Click line 2, press Shift, and click line 5.

- Press Ctrl+Enter to run the selected lines of code.
The four lines of code run and the results are shown in the Console pane. The libraries are now available for the current R session.
Open a public feature service
Next, you'll work with a layer of street trees in Washington, D.C. This data is published by the Urban Forestry Administration and is available from Open Data DC.
- Go to the DC Trees details page in ArcGIS Online.

The item ID is a unique identifier for this layer in ArcGIS Online that can be found in the URL of the details page. You'll use it to access the data.
- In the browser address bar, highlight the item ID in the URL.
Note:
The item ID is the string of numbers of letters following id=.

- Press Ctrl+C to copy the item ID.
- In your IDE, in the script,
press Enter to add a new line. Copy and paste the following code into the script:
trees_iid <- "08bda06465a74cd59a6d4231d882588d"This code sets the variable trees_iid to a string that contains the item ID.

- In the script,
press Enter to add a new line. Copy and paste the following code into the script:
trees_layer <- arc_open(trees_iid)The arc_open() function returns descriptive information about the service and its properties. Printing the trees_layer object, or viewing it in the Variables pane, can help you determine what data to fetch.
- Select lines 6 and 7 and press Ctrl+Enter to run the selected lines of code.

- If the Variables pane is not visible, click View and choose Variables.

- In the Variables pane, expand trees_layer.

More details about the layer appear.

Explore the layer source
You'll inspect the feature layer that you accessed to determine its source and accompanying layers.
- In a new line of your script, copy and paste the following code:
trees_layer$url - Press Ctrl+Enter to run the current line of code.
In the console, the source URL for the trees_layer object appears.

The URL, also known as a REST endpoint, indicates that this is layer number 11. The URL to the MapServer source can be accessed by removing the layer number at the end of the URL.
- In a new line of your script, copy and paste the following code to investigate the MapServer source and view additional layers:
mapserver_url <- dirname(trees_layer$url) dc_mapserver <- arc_open(mapserver_url) dc_mapserver$layers[,c("id", "name")] |> as_tibble() |> arrange(id) |> print(n=30)
- Select lines 9 through 14. Press Ctrl+Enter to run the selected lines of code.

The code runs and the information about the layers is printed to the Console pane.
- Investigate the additional feature layers for this MapServer in the Console pane.

Layer 11 corresponds to the DC Trees layer. As an urban forester, you may want to access additional layers from this MapServer for future analyses.
Inspect the layer's fields
Before you download data to work with in R, it's a good idea to explore the data's fields. The list_fields() function returns an R tibble that describes each attribute column, including its name, alias, type, and other information. Knowing these field properties helps you understand which columns to request and how to use them in a SQL where clause.
- In a new line of your script, copy and paste the following code:
list_fields(trees_layer) |> # pass `n = Inf` to print all rows print(n = Inf) - Select lines 15 through 17. Press Ctrl+Enter to run the selected lines of code.

The field information is printed in the Console pane.

You're only interested in five of these fields: TREE_ID, COMMON_NAME, SCIENTIFIC_NAME, GENUS_NAME, and WARD. It would slow down your processing and be wasteful of server resources, bandwidth, and storage to download the entire sent of columns, so you'll limit your request to these fields.
- In a new line of your script, copy and paste the following code:
relevant_fields <- c( "TREE_ID", "COMMON_NAME", "SCIENTIFIC_NAME", "GENUS_NAME", "WARD" ) - Select lines 18 through 24. Press Ctrl+Enter to run the selected lines of code.

Read features into R
Next, you'll retrieve the features from the server and read them into R. You're only interested in oak trees, so you''ll include a SQL where expression to limit the selection to the genus Quercus.
- In a new line of your script, copy and paste the following code:
oaks <- arc_select( trees_layer, fields = relevant_fields, where = "GENUS_NAME = 'Quercus'", geometry = FALSE )This block of code calls the arc_select() function with four parameters:
- trees_layer is the target feature service.
- fields takes a vector of field names you want to select.
- where is an optional SQL where clause that lets you subset rows.
- geometry = FALSE ensures that no geometry is returned. This parameter is useful for exploring the attributes before querying geometries, which can take longer to download.
- Select lines 25 through 30. Press Ctrl+Enter to run the selected lines of code.

The Console pane shows the selection expression and a progress bar showing the data download progress.

After the download is complete, the progress bar disappears. Next, you'll check how many features were returned.
- In a new line of your script, copy and paste the following code:
nrow(oaks) - Select the new line. Press Ctrl+Enter to run the selected line of code.

The Console pane shows the number of rows.

The query returned almost 200,000 features. To streamline the task of oak inspection assignment, you'll divide this task by administrative areas of operation, starting by limiting the query to a single ward. To do this, you'll modify the SQL where clause to add an additional condition with AND to filter to Ward 2.
- In the script, find line 28, the where clause.

- Edit the where clause line to the following code:
where = "GENUS_NAME = 'Quercus' AND WARD = '2'",
- Delete line 29 (the geometry = FALSE parameter).

- Select lines 25 through 30. Press Ctrl+Enter to run the selected lines of code.

The selection with the modified query runs and downloads the features for oaks in Ward 2.

The number of rows returned by the modified query is 14,278. This number will make the task of assigning arborists more manageable.
Inspect and plot the records
Next, you'll inspect the data to ensure the returned records look correct. Because the arc_select() function returns an sf object, you can view the data interactively in Positron and plot it with base R or any other spatial R package.
- In the Variables pane, expand the Data section and expand oaks section.

- Click the View Data Table button.

The data table appears.

The column names match the names you specified in your relevant_fields variable and the data in each column is consistent with what you expect. Next, you'll use the mapgl library to make a plot of the data.
- In a new line of your script, copy and paste the following code:
mapgl::maplibre_view(oaks, column = "COMMON_NAME")
- Select the new line. Press Ctrl+Enter to run the selected line of code.
In the Variables pane, a Plots section appears. The appearance of the plot may vary based on the window and pane size and zoom level. The returned points fall in Ward 2 and represent many oak trees across the ward.
- Point to the map and use the mouse scroll wheel to zoom in.
- Click one of the tree points to see its attributes.

In this tutorial, you connected to an ArcGIS Online feature service, explored its properties and fields, and pulled a subset of data into R. This pattern of using arc_open(), list_fields(), and arc_select() works with any ArcGIS Online feature service, not just publicly shared ones. With the addition of authentication code, you can access protected items as well.
You can find more tutorials in the tutorial gallery.

