The arcpyr package provides a simple interface to arcpy for R users. The interface should be straightforward to those who have dabbled with Python and the arcpy module, or to those who have done some basic work with ArcGIS Model Builder. If you don't fall into either of those categories, don't worry! This vignette will show you the basics.

Connecting to ArcGIS

The first step is to connect to ArcGIS. If you installed ArcGIS and Python to the default locations, arcpyr should be able to find what it needs automatically. Otherwise, you might need to specify some folder locations. If you want to connect to ArcGIS Pro, make sure to do connect_ArcGIS(Pro = TRUE).

library(arcpyr)
connect_ArcGIS()

Creating the arcpy environment object

Once you have connected to ArcGIS, the next step is to create the arcpy environment object. This object will contain all the ArcGIS tools you want to access from R.

arcpy = arcpy_env()

The code above creates an environment that contains all of the tools that are accessible from you ArcGIS installation. In addition, it also creates an interface to the ArcGIS environment settings.

ls(arcpy)         # list available tools
ls(arcpy$env)     # list available environment settings

Don't need all the tools? There are two ways to create a partial interface.

Method 1: Specify the tools and settings you want

If you only need a few tools or environment settings, you can list them in the call to arcpy_env.

arcpy = arcpy_env(
  tool.list = c("Delete_managment", "Buffer_analysis"),
  env.list = c("workspace", "scratchWorkspace")
)
arcpy$Delete_managment("path/to/layer")

Note that if you're using tools from an ArcGIS extension, you'll need to explicitly check out that extension via checkout_extension().

Method 2: Create an empty environment, and then add toolboxes

If you're going to be working extensively with only a few toolboxes, you can create an empty arcpy environment and then add the toolboxes individually.

arcpy = arcpy_env(NULL) # Access all environment settings, but no tools
attach_toolbox(arcpy, "management")  # attach the management toolbox
attach_toolbox(arcpy, "statistics")  # attach the statistics toolbox

Adding toolboxes in this way will automatically check out any necessary extensions. Note that accessing the tools this way works a bit differently from the first method:

arcpy$management$Delete("path/to/layer")

This may seem strange, but it's completely analogous to the different ways of accessing arcpy tools in Python:

```{python python-import, eval = FALSE}

method 1

import arcpy arcpy.Delete_management('path/to/layer')

method 2

import arcpy.management arcpy.management.Delete('path/to/layer')

### Method 3 (not recommended): attach the environment

If you want to work with the `arcpy` tools directly in you environment,
you can use `attach`:

```r
attach(arcpy_env())
Delete_management('path/to/layer')
env$workspace()

Which is analagous to

```{python import-alt, eval = FALSE} from arcpy import * Delete_management('path/to/layer') env.workspace

in Python. Note that this can clutter your environment and lead to some naming 
conflicts, so I don't generally recommend it.

## Specify ArcGIS environment settings

Setting up the ArcGIS environment with `arcpyr` is only slightly 
different from Python. In `arcpyr`, environment settings are accessed 
via function calls.

```r
arcpy = arcpy_env()
arcpy$env$workspace()                     # get workspace location
arcpy$env$workspace("path/to/workspace")  # set workspace location

Need more?

The arcpyr package focuses on providing an interface to ArcGIS toolboxes. It can't handle the object-oriented aspects of arcpy and Python, like doing raster calculations reading/writing attribute tables. If you need that sort of functionality, check out the arcpyrextra package.



mkoohafkan/arcpyr documentation built on May 23, 2019, 2:01 a.m.