library(httptest)
start_vignette("vignette-name")

Introduction to dkanr

The dkanr package is an R client to the DKAN REST API. dkanr implements all the methods available via the DKAN REST API and DKAN datastore API. Additionnally, it provides a few wrapper functions to facilitate interacting with the DKAN API from R.

In this brief introduction, we will see how to download data from a specific data set. In the process, we will see how to:

  1. Set-up a connection
  2. Locate a dataset
  3. Access its metadata
  4. Download the attached data

Step 1: Setting Up Your Connection

Connection without authentication

To set-up a connection without authentication, you just need the site URL.

library(purrr)
library(dkanr)
library(dplyr)
dkanr_setup(url = 'https://data.louisvilleky.gov')

Authenticated connection

If authentication is required, you will also need to provide a valid username and password.

dkanr_setup(url = 'http://demo.getdkan.com',
            username = 'my_username',
            password = 'my_password')

You can verify that you are successfully connected by printing your connection information.

dkanr_settings()

Step 2: List all available datasets with list_nodes_all()

While exploring the offerings of a catalog, you can retrieve all the available datasets with a simple query.

# Get a list of all datasets
resp <- list_nodes_all(filters = c(type = 'dataset'), as = 'df')
# Print the first 10 datasets
resp %>%
  select(nid, title, uri) %>%
  arrange(title) %>%
  head(n = 10)

Step 3: Access metadata for a specific dataset node

Say you are interested in a specific dataset from the catalog, for instance, the "Active Permits" dataset. You can easily retrieve this dataset metadata using the dataset node ID.

  1. First, identify the dataset node ID
  2. Then, use the node ID to retrieve the dataset metadata

Identify the dataset node ID

# Print only the "Active Permits" dataset information
resp %>%
  filter(title == 'Active Permits') %>%
  select(nid, title, uri, type)

Use the node ID to retrieve the dataset metadata

metadata <- retrieve_node(nid ='8216', as = 'list')
metadata
# All metadata fields
names(metadata)[1:30]
# Access specific metadata fields
metadata$title

Step 4: Access data for a specific resource node

Once you have identified a dataset of interest, you will probably want to download actual data. Multiple data files and documents may be attached to a single dataset, so you'll first need to list all the resources (data files, and other type of documents) that are linked to the dataset you are interested in.

Here, a single resource is attached to the "Active Permits" dataset

get_resource_nids(metadata)

You can then use the resource node ID to retrieve its metadata.

metadata_rs <- retrieve_node(nid ='8221', as = 'list')
metadata_rs

Data can then be dowloaded either as

Batch download

Retrieve the resource URL from the resource metadata

get_resource_url(metadata_rs)

API call

Some data files may be directly queried through the API. Only data files that have been imported into the DKAN datastore can be queried through the API.

First, you'll need to check if the data file you are interested in is available from the DKAN datastore

ds_is_available(metadata_rs)

If this is the case, you'll be able to retrieve data directly from the datastore. In order to do so, you'll have to use the resource UUID (Just another unique ID number)

ds_search_all(resource_id = metadata_rs$uuid, as = 'df') %>%
  select(PERMITNUMBER, PERMITTYPE, STATUS, SQUAREFEET)
temp <- ds_search(resource_id = metadata_rs$uuid)
dplyr::bind_rows(temp) %>%
  select(PERMITNUMBER, PERMITTYPE, STATUS, SQUAREFEET)
end_vignette()


tonyfujs/dkanr documentation built on May 28, 2019, 4:03 p.m.