diff --git a/NAMESPACE b/NAMESPACE index 369705a..d1d7ef5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(get_dataset) +export(get_latest_resource_id) export(get_resource) export(get_resource_sql) importFrom(magrittr,"%>%") diff --git a/R/get_latest_resource.R b/R/get_latest_resource.R deleted file mode 100644 index 772fedf..0000000 --- a/R/get_latest_resource.R +++ /dev/null @@ -1,5 +0,0 @@ - -get_latest_resource <- function(resrouce_id){ - ckanr::ckanr_setup("www.opendata.nhs.scot") - ckanr::resource_show(resrouce_id) -} diff --git a/R/get_latest_resource_id.R b/R/get_latest_resource_id.R new file mode 100644 index 0000000..7c6254d --- /dev/null +++ b/R/get_latest_resource_id.R @@ -0,0 +1,66 @@ +#' Get the latest Open Data resource ID associated +#' with a dataset +#' +#' @description Returns the ID of the resource +#' that was uploaded to a dataset most recently. +#' This ID can be used as a parameter within +#' [get_resource()] +#' +#' @param dataset_name name of the dataset as found on +#' \href{https://www.opendata.nhs.scot/}{NHS Open Data platform} +#' +#' @importFrom magrittr %>% +#' @return a string of the resource ID that was uploaded to +#' the dataset most recently. +#' +#' @export + + +get_latest_resource_id <- function(dataset_name){ + ckanr::ckanr_setup("www.opendata.nhs.scot") + + check_dataset_name(dataset_name) + + # define query and try API call + query <- paste0("id=", dataset_name) + content <- try( + phs_GET("package_show", query), + silent = TRUE + ) + + # if content contains a 'Not Found Error' + # throw error with suggested dataset name + if (grepl("Not Found Error", content[1])) { + suggest_dataset_name(dataset_name) + } + + + package <- ckanr::package_show(dataset_name) + resources = c() + updated_dates = c() + resource_name = c() + + for (resource in package$resources){ + resources <- append(resources, resource$id) + updated_dates <- append(updated_dates, resource$last_modified) + resource_name <- append(resource_name, resource$name)} + + + package_resources <- tibble::tibble("resource_id" = resources, "updated_date" = updated_dates, + "resource_name" = resource_name) + + latest_resource <- package_resources %>% + dplyr::filter(updated_date == max(updated_date)) + + latest_resource_id <- latest_resource %>% dplyr::pull(resource_id) + + return(latest_resource_id) +} + + + + + + + + diff --git a/man/get_latest_resource_id.Rd b/man/get_latest_resource_id.Rd new file mode 100644 index 0000000..398a220 --- /dev/null +++ b/man/get_latest_resource_id.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_latest_resource_id.R +\name{get_latest_resource_id} +\alias{get_latest_resource_id} +\title{Get the latest Open Data resource ID associated +with a dataset} +\usage{ +get_latest_resource_id(dataset_name) +} +\arguments{ +\item{dataset_name}{name of the dataset as found on +\href{https://www.opendata.nhs.scot/}{NHS Open Data platform}} +} +\value{ +a string of the resource ID that was uploaded to +the dataset most recently. +} +\description{ +Returns the ID of the resource +that was uploaded to a dataset most recently. +This ID can be used as a parameter within +\code{\link[=get_resource]{get_resource()}} +}