ckanr (version 0.7.0)

resource_update: Update a resource

Description

This function can be used to update a resource's file attachment and "extra" metadata fields. Any update will also set the metadata key "last_updated". Other metadata, such as name or description, are not updated.

The new file must exist on a local path. R objects have to be written to a file, e.g. using tempfile() - see example.

For convenience, CKAN base url and API key default to the global options, which are set by ckanr_setup.

Usage

resource_update(
  id,
  path = NULL,
  extras = list(),
  url = get_default_url(),
  key = get_default_key(),
  as = "list",
  ...
)

Value

The HTTP response from CKAN, formatted as list (default), table, or JSON.

Arguments

id

(character) Resource ID to update (required)

path

(character) Local path of the file to upload (optional)

extras

(list) - the resources' extra metadata fields (optional)

url

Base url to use. Default: https://data.ontario.ca/ See also ckanr_setup and get_default_url.

key

A privileged CKAN API key, Default: your key set with ckanr_setup

as

(character) One of list (default), table, or json. Parsing with table option uses jsonlite::fromJSON(..., simplifyDataFrame = TRUE), which attempts to parse data to data.frame's when possible, so the result can vary from a vector, list or data.frame. (required)

...

Curl args passed on to verb-POST (optional)

References

http://docs.ckan.org/en/latest/api/index.html#ckan.logic.action.create.resource_create

Examples

Run this code
if (FALSE) {
ckanr_setup(url = "https://demo.ckan.org/", key = getOption("ckan_demo_key"))

# Get file
path <- system.file("examples", "actinidiaceae.csv", package = "ckanr")

# Create package, then a resource within that package
(res <- package_create("newpackage10"))
(xx <- resource_create(package_id = res$id,
                       description = "my resource",
                       name = "bears",
                       upload = path,
                       rcurl = "http://google.com"
))

# Modify dataset, here lowercase strings in one column
dat <- read.csv(path, stringsAsFactors = FALSE)
dat$Family <- tolower(dat$Family)
newpath <- tempfile(fileext = ".csv")
write.csv(dat, file = newpath, row.names = FALSE)

# Upload modified dataset
## Directly from output of resource_create
resource_update(xx, path=newpath)

## or from the resource id
resource_update(xx$id, path=newpath)

## optionally include extra tags
resource_update(xx$id, path=newpath,
                extras = list(some="metadata"))
                
# Update a resource's extra tags
## add extra tags without uploading a new file
resource_update(id,
                extras = list(some="metadata"))

## or remove all extra tags
resource_update(id, extras = list())                 

#######
# Using default settings
ckanr_setup(url = "http://demo.ckan.org/", key = "my-demo-ckan-org-api-key")
path <- system.file("examples", "actinidiaceae.csv", package = "ckanr")
resource_update(id="an-existing-resource-id", path = path)

# Using an R object written to a tempfile, and implicit CKAN URL and API key
write.csv(data <- installed.packages(), path <- tempfile(fileext = ".csv"))
ckanr_setup(url = "http://demo.ckan.org/", key = "my-demo-ckan-org-api-key")
resource_update(id="an-existing-resource-id", path = path)

# Testing: see ?ckanr_setup to set default test CKAN url, key, package id
ckanr_setup(test_url = "http://my-ckan.org/",
            test_key = "my-ckan-api-key",
            test_did = "an-existing-package-id",
            test_rid = "an-existing-resource-id")
resource_update(id = get_test_rid(),
                path = system.file("examples",
                                   "actinidiaceae.csv",
                                   package = "ckanr"),
                key = get_test_key(),
                url = get_test_url())

# other file formats
## html
path <- system.file("examples", "mapbox.html", package = "ckanr")

# Create package, then a resource within that package
(res <- package_create("mappkg"))
(xx <- resource_create(package_id = res$id,
                       description = "a map, yay",
                       name = "mapyay",
                       upload = path,
                       rcurl = "http://google.com"
))
browseURL(xx$url)

# Modify dataset, here lowercase strings in one column
dat <- readLines(path)
dat <- sub("-111.06", "-115.06", dat)
newpath <- tempfile(fileext = ".html")
cat(dat, file = newpath, sep = "\n")

# Upload modified dataset
## Directly from output of resource_create
(xxx <- resource_update(xx, path=newpath))
browseURL(xxx$url)
}

Run the code above in your browser using DataLab