From d677eaa6e6ca222094b1cff352779c06de50e4f8 Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Sat, 14 Dec 2024 15:35:40 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Use=20available=20metadata=20when?= =?UTF-8?q?=20creating=20a=20data=20package?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit data_package() uses the metadata generated by update_metadata() --- R/data_package.R | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/R/data_package.R b/R/data_package.R index 3d0ac35..e5148cc 100644 --- a/R/data_package.R +++ b/R/data_package.R @@ -49,8 +49,8 @@ data_resource <- function(file, path = ".") { clean_data_path(root = path, file = file)[2] |> read_yaml() -> metadata list( - name = file, path = file, "encoding" = "utf-8", - format = "csv", media_type = "text/csv", + name = coalesce(metadata[["..generic"]][["name"]], file), path = file, + "encoding" = "utf-8", format = "csv", media_type = "text/csv", hash = paste0("sha1:", metadata[["..generic"]][["data_hash"]]), schema = list( fields = vapply( @@ -62,12 +62,15 @@ data_resource <- function(file, path = ".") { c(value = metadata[["..generic"]][["NA string"]], label = "missing") ) ) - ) |> + ) -> dr + extra <- c("title", "description") + metadata[["..generic"]][extra[extra %in% names(metadata[["..generic"]])]] |> + c(dr) |> list() } field_schema <- function(x, metadata) { - list(switch( + switch( metadata[[x]]$class, "character" = list(name = x, type = "string"), "Date" = list(name = x, type = "date"), @@ -81,7 +84,24 @@ field_schema <- function(x, metadata) { ), "integer" = list(name = x, type = "integer"), "numeric" = list(name = x, type = "number"), - "POSIXct" = list(name = x, type = "datetime"), + "POSIXct" = list( + name = x, type = "datetime", format = "%Y-%m-%dT%H:%M:%SZ" + ), stop("field_schema() can't handle ", metadata[[x]]$class) - )) + ) -> fs + if ("description" %in% names(metadata[[x]])) { + fs$description <- metadata[[x]][["description"]] + } + return(list(fs)) +} + +coalesce <- function(...) { + dots <- list(...) + if (length(dots) == 0) { + return(NULL) + } + if (!is.null(dots[[1]])) { + return(dots[[1]]) + } + coalesce(dots[-1]) }