library(httptest) start_vignette("vignette-name")
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:
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')
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()
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)
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.
# Print only the "Active Permits" dataset information resp %>% filter(title == 'Active Permits') %>% select(nid, title, uri, type)
metadata <- retrieve_node(nid ='8216', as = 'list') metadata
# All metadata fields names(metadata)[1:30]
# Access specific metadata fields metadata$title
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
Retrieve the resource URL from the resource metadata
get_resource_url(metadata_rs)
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()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.