From 10c4d82c8fa5e12a7a0062784475a7353306198c Mon Sep 17 00:00:00 2001 From: antaldaniel Date: Wed, 29 Nov 2023 12:20:33 +0100 Subject: [PATCH] change misleading source() to datasource() --- R/datasource.R | 58 ++++++++++++++++++++++++ man/{dataset_source.Rd => datasource.Rd} | 22 +++------ tests/testthat/test-datasource.R | 11 +++++ 3 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 R/datasource.R rename man/{dataset_source.Rd => datasource.Rd} (66%) create mode 100644 tests/testthat/test-datasource.R diff --git a/R/datasource.R b/R/datasource.R new file mode 100644 index 0000000..9a172a2 --- /dev/null +++ b/R/datasource.R @@ -0,0 +1,58 @@ +#' @title Get/set the Source of the object. +#' @description Get/set the optional \code{Source} property as an attribute to an +#' R object. Do not confuse with the base R \code{source()} function. +#' @details The \code{Source} is a related resource from which the described resource is +#' derived. See \href{https://purl.org/dc/elements/1.1/source}{dct:source}. In Datacite, +#' the source is described by a \code{relatedIdentifierType} with the property +#' \code{relationType="isDerivedFrom"}. +#' @param value The \code{Source} as a character string of lengths one. +#' @inheritParams dublincore +#' @return The \code{Source} attribute as a character of length 1 is added to \code{x}. +#' @examples +#' datasource(iris_dataset) <- "https://doi.org/10.1111/j.1469-1809.1936.tb02137.x" +#' datasource(iris_dataset) +#' @family Reference metadata functions +#' @export +datasource <- function(x) { + + assertthat::assert_that(is.dataset(x), + msg = "datasource(x) must be a dataset object created with dataset() or as_dataset().") + DataBibentry <- dataset_bibentry(x) + DataBibentry$source + +} + +#' @rdname dataset_source +#' @export +`datasource<-` <- function(x, overwrite = TRUE, value) { + + assertthat::assert_that(is.dataset(x), + msg = "datasource(x): x must be a dataset object created with dataset() or as_dataset().") + + DataBibentry <- invisible(dataset_bibentry(x)) + + if ( is.null(value) ) { + DataBibentry$source <- ":unas" + attr(x, "DataBibentry") <- DataBibentry + return(invisible(x)) + } + + if (length(value)>1) { + stop("source(x) <- value: value must be of length 1.") + } + + is_unas <- DataBibentry$source == ":unas" + + if (is.null(DataBibentry$source)) { + DataBibentry$source <- value + } else if (is_unas) { + DataBibentry$source <- value + }else if ( overwrite ) { + DataBibentry$source <- value + } else { + message ("The dataset has already a source: ", DataBibentry$source ) + } + + attr(x, "DataBibentry") <- DataBibentry + invisible(x) +} diff --git a/man/dataset_source.Rd b/man/datasource.Rd similarity index 66% rename from man/dataset_source.Rd rename to man/datasource.Rd index 39bed67..e574277 100644 --- a/man/dataset_source.Rd +++ b/man/datasource.Rd @@ -1,20 +1,13 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/source.R -\name{dataset_source} -\alias{dataset_source} -\alias{dataset_source<-} +% Please edit documentation in R/datasource.R +\name{datasource} +\alias{datasource} \title{Get/set the Source of the object.} \usage{ -dataset_source(x) - -dataset_source(x, overwrite = TRUE) <- value +datasource(x) } \arguments{ -\item{x}{An R object of type data.frame, or inherited data.table, tibble; alternatively a well -structured R list.} - -\item{overwrite}{If pre-existing metadata properties should be overwritten, -defaults to \code{TRUE}.} +\item{x}{An object that is tested if it has a class "dublincore".} \item{value}{The \code{Source} as a character string of lengths one.} } @@ -32,9 +25,8 @@ the source is described by a \code{relatedIdentifierType} with the property \code{relationType="isDerivedFrom"}. } \examples{ -iris_dataset <- iris -dataset_source(iris_dataset) <- "https://doi.org/10.1111/j.1469-1809.1936.tb02137.x" -dataset_source(iris_dataset) +datasource(iris_dataset) <- "https://doi.org/10.1111/j.1469-1809.1936.tb02137.x" +datasource(iris_dataset) } \seealso{ Other Reference metadata functions: diff --git a/tests/testthat/test-datasource.R b/tests/testthat/test-datasource.R new file mode 100644 index 0000000..6e4a8d9 --- /dev/null +++ b/tests/testthat/test-datasource.R @@ -0,0 +1,11 @@ +datasource(x=iris_dataset) <- NULL + +test_that("datasource() initializes :unas unassigned value", { + expect_equal(datasource(iris_dataset), ":unas") +}) + +datasource(iris_dataset) <- "https://doi.org/10.1111/j.1469-1809.1936.tb02137.x" + +test_that("dataset_source() <- assignment works", { + expect_equal(datasource(iris_dataset), "https://doi.org/10.1111/j.1469-1809.1936.tb02137.x") +})