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.

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

  2. On the Welcome page, click New Folder.

    New Folder button

  3. In the Folder Template window, click R Project.

    R Project button

  4. Click Next.
  5. In the New Folder From Template window, for Folder Name, type my-r-project.
  6. 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.

    Folder Name and Location pane

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

    Project Configuration window with R version

    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.

  9. Click the Create button.
  10. In the New Folder Created window, click Current Window.

    Current Window button

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

    New File button

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

    New file with name

    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.

  13. Copy and paste the following code into the script window:

    install.packages(c("arcgis", "sf", "dplyr", "mapgl"))

  14. Click the line number 1 to select the line of code.

    Line number 1

  15. Press Ctrl+Enter to run the selected line of code.

    The code runs and the Console pane shows the results.

    Console window with 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.

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

  17. Click line 2, press Shift, and click line 5.

    Lines 2 through 5

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

  1. Go to the DC Trees details page in ArcGIS Online.

    DC Trees details page

    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.

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

    Item ID in the URL

  3. Press Ctrl+C to copy the item ID.
  4. 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.

    Item ID set to trees_iid

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

  6. Select lines 6 and 7 and press Ctrl+Enter to run the selected lines of code.

    Lines 6 and 7

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

    Variables option on the View menu

  8. In the Variables pane, expand trees_layer.

    Button to expand trees_layer

    More details about the layer appear.

    Variables pane

Explore the layer source

You'll inspect the feature layer that you accessed to determine its source and accompanying layers.

  1. In a new line of your script, copy and paste the following code:

    trees_layer$url

  2. Press Ctrl+Enter to run the current line of code.

    In the console, the source URL for the trees_layer object appears.

    URL for trees_layer

    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.

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

    Code to inspect the MapServer layers

  4. Select lines 9 through 14. Press Ctrl+Enter to run the selected lines of code.

    Lines 9 through 14

    The code runs and the information about the layers is printed to the Console pane.

  5. Investigate the additional feature layers for this MapServer in the Console pane.

    Feature layers from the MapServer source

    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.

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

  2. Select lines 15 through 17. Press Ctrl+Enter to run the selected lines of code.

    List fields code

    The field information is printed in the Console pane.

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

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

  4. Select lines 18 through 24. Press Ctrl+Enter to run the selected lines of code.

    Code to define the relevant fields

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.

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

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

    Oaks selecting code

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

    Console pane with progress bar

    After the download is complete, the progress bar disappears. Next, you'll check how many features were returned.

  3. In a new line of your script, copy and paste the following code:

    nrow(oaks)

  4. Select the new line. Press Ctrl+Enter to run the selected line of code.

    Line of code to count the rows

    The Console pane shows the number of rows.

    Number of oaks in the Console pane

    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.

  5. In the script, find line 28, the where clause.

    Line 28

  6. Edit the where clause line to the following code:

      where = "GENUS_NAME = 'Quercus' AND WARD = '2'",

    Modified where clause

  7. Delete line 29 (the geometry = FALSE parameter).

    Code without the geometry = FALSE parameter

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

    Modified query

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

    Console pane with the modified selection results

    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.

  1. In the Variables pane, expand the Data section and expand oaks section.

    Data and oaks sections

  2. Click the View Data Table button.

    View Data Table button

    The data table appears.

    Data table

    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.

  3. In a new line of your script, copy and paste the following code:

    mapgl::maplibre_view(oaks, column = "COMMON_NAME")

    Code to plot the rows

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

  5. Point to the map and use the mouse scroll wheel to zoom in.
  6. Click one of the tree points to see its attributes.

    Tree plot with pop-up

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.