Package 'dkanr'

Title: Client for the 'DKAN' API
Description: Provides functions to facilitate access to the 'DKAN' API (<https://dkan.readthedocs.io/en/latest/apis/index.html>), including the 'DKAN' REST API (metadata), and the 'DKAN' datastore API (data). Includes functions to list, create, retrieve, update, and delete datasets and resources nodes. It also includes functions to search and retrieve data from the 'DKAN' datastore.
Authors: Tony Fujs [aut, cre], Meera Desai [aut], Karthik Ramanathan [aut]
Maintainer: Tony Fujs <[email protected]>
License: MIT + file LICENSE
Version: 0.1.3
Built: 2025-01-14 06:22:01 UTC
Source: CRAN

Help Index


attach_file_to_node

Description

Attach a file to an existing resource node

Usage

attach_file_to_node(nid, url = get_url(), credentials = list(cookie =
  dkanr::get_cookie(), token = dkanr::get_token()), file_path,
  attach = 1)

Arguments

nid

character: DKAN resource node ID

url

character: The DKAN site URL

credentials

Optional list parameter. Default values are Cookie and Token generated by dkan_setup()

file_path

character: path to file on local machine

attach

numeric: Setting the attach parameter to 0 ensures that the file will replace any existing file on the resource. Setting it to 1 will result in a rejected request if the resource already has an attached file (but it will work if the resource file upload field is empty).


create_node

Description

Create a DKAN node

Usage

create_node(url = get_url(), body, credentials = list(cookie =
  dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)

Arguments

url

character: The DKAN site URL

body

json: A JSON object to be passed to the body of the request

credentials

Optional list parameter. Default values are Cookie and Token generated by dkan_setup()

as

character: Output format. Options are: 'json' or 'list'

...

Other optional parameters passed to the underlying GET request

Value

dkan_node object

Examples

## Not run: 
create_node(url = "http://demo.getdkan.com", body = {"title": "TEST DATASET"})

## End(Not run)

delete_node

Description

Delete an existing DKAN node

Usage

delete_node(nid, url = get_url(), credentials = list(cookie =
  dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)

Arguments

nid

character: DKAN node ID

url

character: The DKAN site URL

credentials

Optional list parameter. Default values are Cookie and Token generated by dkan_setup()

as

character: Output format. Options are: 'json' or 'list'

...

Other optional parameters passed to the underlying GET request

Value

character or dkan_node object

Examples

## Not run: 
delete_node(nid = 1, url = "http://demo.getdkan.com")

## End(Not run)

dkanr_logout

Description

Close connection with DKAN API initiated by dkanr_setup()

Usage

dkanr_logout(url = get_url(), credentials = list(cookie =
  dkanr::get_cookie(), token = dkanr::get_token()))

Arguments

url

Character: API root URL. Default value is the url returned by get_url()

credentials

Optional list parameter. Default values are Cookie and Token generated by dkan_setup()

Value

json


Get or set dkanr DKAN settings

Description

Get or set dkanr DKAN settings

Usage

dkanr_settings()

get_url()

get_cookie()

get_token()

Value

dkanr_settings prints your base url, API cookie and token (if used) dkanr_setup sets your production settings

See Also

dkanr_setup, get_url

Examples

dkanr_settings()

Configure default DKAN settings

Description

Configure default DKAN settings

Usage

dkanr_setup(url = "http://demo.getdkan.com", username = NULL,
  password = NULL)

Arguments

url

A DKAN URL (optional), default: "http://demo.getdkan.com"

username

Username (optional, character)

password

Password (optional, character)

Details

dkanr_setup sets DKAN connection details. dkanr functions default to use the default URL and API key unless specified explicitly.

Examples

## Not run: 
DKAN users without admin/editor privileges could run:
dkanr_setup(url = "http://demo.getdkan.com")

Privileged DKAN editor/admin users can run:
dkanr_setup(url = "http://demo.getdkan.com", username = "your-username", password = "your-password")

Not specifying the default DKAN URL will reset the DKAN URL to its default
"http://demo.getdkan.com":
dkanr_setup()

## End(Not run)

ds_is_available

Description

Returns TRUE is a data file has been imported in the DKAN datastore, returns FALSE otherwise

Usage

ds_is_available(metadata)

Arguments

metadata

list: Resource level metadata

Value

logical vector

Examples

## Not run: 
metadata <- retrieve_node(nid ='140366', as = 'list')
ds_is_available(metadata)

## End(Not run)

ds_search_all

Description

Search csv resource file stored in the datastore.

Usage

ds_search_all(resource_id, fields = NULL, filters = NULL,
  num_records = NULL, offset = 0, sort = NULL, q = NULL,
  url = get_url(), credentials = list(cookie = dkanr::get_cookie(),
  token = dkanr::get_token()), as = "df")

Arguments

resource_id

character: DKAN resource ID

fields

character vector: fields to be returned by the request

filters

named character vector: filters to be applied when retrieving records

num_records

numeric: Number of records to retrieve

offset

numeric: Number of results to skip in the beginning

sort

named character vector: field to be sorted by and the order of sorting

q

character: full text query filter

url

character: The DKAN site URL

credentials

Optional list parameter. Default values are Cookie and Token generated by dkan_setup()

as

character: output format - json, list or data frame

Value

character, list, or data frame

Examples

## Not run: 
ds_search_all(resource_id = '10c578a6-63c4-40bd-a55d-0c27bf276283',
          fields = c('Country','City','Region','Population'),
          filters = list('Country'=c('co','my'), 'Region'=c('04','09','22')),
          num_records = 20,
          offset = 0,
          limit = 100,
          sort = c('Country'='asc'),
          q = "puertica",
          url = get_url(),
          as = 'df')

## End(Not run)

err_handler

Description

Helper function to capture error messages returned by the API

Usage

err_handler(x)

Arguments

x

response: The API response


filters_to_text_query

Description

Helper function to build API query string

Usage

filters_to_text_query(filters, text)

Arguments

filters

named vector: filters to be applied

text

vector: The key word used for filtering


get_resource_nids

Description

Returns all the resource node ids attached to a specific node id

Usage

get_resource_nids(metadata)

Arguments

metadata

list: Dataset level metadata

Value

character vector

Examples

## Not run: 
metadata <- retrieve_node(nid ='140177', as = 'list')
get_resource_nids(metadata)

## End(Not run)

get_resource_url

Description

Returns the download link or external page URL for a specific resource

Usage

get_resource_url(metadata)

Arguments

metadata

list: Resource level metadata

Value

character vector

Examples

## Not run: 
metadata <- retrieve_node(nid ='140366', as = 'list')
get_resource_url(metadata)

## End(Not run)

dkan_list class helpers

Description

dkan_list class helpers

Usage

is.dkan_list(x)

Arguments

x

Variety of things, character, list, or dkan_list class object as.dkan_list <- function(x, ...) UseMethod("as.dkan_list")

#' @export as.dkan_list.character <- function(x, ...) retrieve_node(x, ...)

#' @export as.dkan_list.dkan_list <- function(x, ...) x

#' @export as.dkan_list.list <- function(x, ...) structure(x, class = "dkan_list")


dkan_node class helpers

Description

dkan_node class helpers

Usage

is.dkan_node(x)

Arguments

x

Variety of things, character, list, or dkan_node class object # create item class from only an item ID as.dkan_node("22")

# gives back itself (x <- as.dkan_node("22")) as.dkan_node(x) as.dkan_node <- function(x, ...) UseMethod("as.dkan_node")

#' @export as.dkan_node.character <- function(x, ...) retrieve_node(x, ...)

#' @export as.dkan_node.dkan_node <- function(x, ...) x

#' @export as.dkan_node.list <- function(x, ...) structure(x, class = "dkan_node")


list_nodes

Description

Returns list of nodes

Usage

list_nodes(url = get_url(), credentials = list(cookie =
  dkanr::get_cookie(), token = dkanr::get_token()), fields = NULL,
  filters = NULL, pagesize = NULL, page = NULL, as = "json", ...)

Arguments

url

character: The DKAN site URL

credentials

Optional list parameter. Default values are Cookie and Token generated by dkan_setup()

fields

character vector: fields to be returned by the request

filters

named character vector: filters to be applied to the search

pagesize

numeric: Number of records to get per page (max = 20).

page

numeric: The zero-based index of the page to get, defaults to 0.

as

character: Output format. Options are: 'json', 'list', or 'df' (data frame)

...

Other optional parameters to be passed to the underlying GET request

Value

json, dkan_node (list), or data frame

Examples

## Not run: 
list_nodes(url = "http://demo.getdkan.com")
list_nodes(url = "http://demo.getdkan.com",
               fields = c('nid', 'type', 'uri'),
               filters = c(type='resource'))
               
## End(Not run)

list_nodes_all

Description

Returns list of nodes

Usage

list_nodes_all(url = get_url(), credentials = list(cookie =
  dkanr::get_cookie(), token = dkanr::get_token()), fields = NULL,
  filters = NULL, as = "df", ...)

Arguments

url

character: The DKAN site URL

credentials

Optional list parameter. Default values are Cookie and Token generated by dkan_setup()

fields

character vector: fields to be returned by the request

filters

named character vector: filters to be applied to the search

as

character: Output format. Options are: 'json', 'list' or 'df' (data frame)

...

Other optional parameters to be passed to the underlying GET request

Value

json, dkan_node (list), or data frame

Examples

## Not run: 
list_nodes_all(url = "http://demo.getdkan.com",
fields = c('nid', 'type'),
filters = c(type = 'resource'))

## End(Not run)

retrieve_node

Description

Returns metadata associated with an existing DKAN Node ID.

Usage

retrieve_node(nid, url = get_url(), credentials = list(cookie =
  dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)

Arguments

nid

character: DKAN node ID

url

character: The DKAN site URL

credentials

Optional list parameter. Default values are Cookie and Token generated by dkan_setup()

as

character: Output format. Options are: 'json' or 'list'

...

Other optional parameters passed to the underlying GET request

Value

dkan_node object

Examples

## Not run: 
retrieve_node(nid = 1, url = "http://demo.getdkan.com")

## End(Not run)

update_node

Description

Update an existing DKAN node

Usage

update_node(nid, url = get_url(), body, credentials = list(cookie =
  dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)

Arguments

nid

character: DKAN node ID

url

character: The DKAN site URL

body

json: A JSON object to be passed to the body of the request

credentials

Optional list parameter. Default values are Cookie and Token generated by dkan_setup()

as

character: Output format. Options are: 'json' or 'list'

...

Other optional parameters passed to the underlying GET request

Value

dkan_node object

Examples

## Not run: 
update_node(nid = 1, url = "http://demo.getdkan.com")

## End(Not run)