diff --git a/NAMESPACE b/NAMESPACE index f7f26e0..c3fe728 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,8 +8,8 @@ S3method(is.subject,subject) S3method(print,dataset) S3method(summary,dataset) export("creator<-") -export("dataset_source<-") export("dataset_title<-") +export("datasource<-") export("description<-") export("geolocation<-") export("identifier<-") @@ -27,10 +27,10 @@ export(dataset_download) export(dataset_export) export(dataset_export_csv) export(dataset_local_id) -export(dataset_source) export(dataset_title) export(dataset_title_create) export(dataset_uri) +export(datasource) export(description) export(dublincore) export(geolocation) diff --git a/R/dataset_bibentry.R b/R/dataset_bibentry.R index 218096c..c1be671 100644 --- a/R/dataset_bibentry.R +++ b/R/dataset_bibentry.R @@ -5,6 +5,8 @@ #' For further information on #' the \code{bibentry} class see \code{utils::\link[utils]{bibentry}}. #' @param ds A dataset object created by \code{\link{dataset}}. +#' @param style Defaults to \code{"text"}, alternatives: \code{"Bibtex"}, +#' \code{"citation"}, \code{"html"}, \code{"R"}. #' @return The \code{\link[utils]{bibentry}} object of the dataset. #' @importFrom utils bibentry #' @examples @@ -23,12 +25,19 @@ #' resourceType = "Dataset" #' ) #' -#' print(ds, style="text") -#' print(ds, style="Bibtex") +#' dataset_bibentry(ds, style="text") +#' dataset_bibentry(ds, style="Bibtex") #' @export -dataset_bibentry <- function(ds) { +dataset_bibentry <- function(ds, style = "text") { - attr(ds, "DataBibentry") + DataBibentry <- attr(ds, "DataBibentry") + if( ! style %in% c("Bibtex", "citation", "html", "latex", "R", "text") ) { + message('dataset_bibentry(ds, style="..." must be any of "text", "Bibtex", "citation",\n"html", "latex", "R". Using "text".') + DataBibentry + } else { + print(DataBibentry, style = style) + invisible(DataBibentry) + } } diff --git a/R/publisher.R b/R/publisher.R index 2b44aa3..4d5802a 100644 --- a/R/publisher.R +++ b/R/publisher.R @@ -22,15 +22,27 @@ #' @family Reference metadata functions #' @export publisher<- function(x) { - attr(x, "Publisher") + + assertthat::assert_that(is.dataset(x), + msg = "publisher(x) must be a dataset object created with dataset() or as_dataset().") + + + DataBibentry <- dataset_bibentry(x) + DataBibentry$publisher } #' @rdname publisher #' @export `publisher<-` <- function(x, overwrite = TRUE, value) { + assertthat::assert_that(is.dataset(x), + msg = "publisher(x) must be a dataset object created with dataset() or as_dataset().") + + DataBibentry <- invisible(dataset_bibentry(x)) + if ( is.null(value)) { - attr(x, "Publisher") <- NA_character_ + DataBibentry$publisher <- ":tba" + attr(x, "DataBibentry") <- DataBibentry return(x) } @@ -38,13 +50,18 @@ publisher<- function(x) { stop("publisher(x) <- value: value must be of length 1.") } - if (is.null(attr(x, "Publisher"))) { - attr(x, "Publisher") <- value - } else if ( overwrite ) { - attr(x, "Publisher") <- value + is_tba <- DataBibentry$publisher == ":tba" + + if (is.null(DataBibentry$publisher)) { + DataBibentry$publisher <- value + } else if (is_tba) { + DataBibentry$publisher <- value + }else if ( overwrite ) { + DataBibentry$publisher <- value } else { - message ("The dataset has already an Publisher: ", publisher(x) ) + message ("The dataset has already an Publisher: ", DataBibentry$publisher ) } + attr(x, "DataBibentry") <- DataBibentry x } diff --git a/tests/testthat/test-dataset_bibentry.R b/tests/testthat/test-dataset_bibentry.R index 0a7643f..78e6bca 100644 --- a/tests/testthat/test-dataset_bibentry.R +++ b/tests/testthat/test-dataset_bibentry.R @@ -13,6 +13,20 @@ ds <- dataset(iris, resourceType = "Dataset" ) -test_that("multiplication works", { +tempcon <- tempfile() + +test_that("dataset_bibentry() works", { expect_equal(class(dataset_bibentry(ds)), "bibentry") + expect_message(dataset_bibentry(ds, style = "cool")) +}) + +writeLines(text = paste(format(dataset_bibentry(ds), "Bibtex"), collapse = "\n\n"), + tempcon ) + +read_bibtex <- readLines(tempcon) + +test_that("dataset_bibentry() works", { + expect_equal(read_bibtex[1], "@Misc{,") + expect_equal(read_bibtex[2], " title = {The iris Dataset},") }) + diff --git a/tests/testthat/test-publisher.R b/tests/testthat/test-publisher.R index 07be99b..d039ada 100644 --- a/tests/testthat/test-publisher.R +++ b/tests/testthat/test-publisher.R @@ -1,17 +1,33 @@ -iris_dataset <- iris -publisher(iris_dataset) <- "American Iris Society" +iris_dataset <- dataset( + x = iris, + title = "Iris Dataset", + author = person("Edgar", "Anderson", role = "aut"), + source = "https://doi.org/10.1111/j.1469-1809.1936.tb02137.x", + date = 1935, + language = "en", + description = "This famous (Fisher's or Anderson's) iris data set." +) +a <- +is.null(a) test_that("publisher() works", { - expect_equal(publisher(iris_dataset), "American Iris Society") - expect_message(publisher(iris_dataset, overwrite=FALSE) <- "Overwritten") + expect_true(is.null(publisher(iris_dataset))) +}) + +iris_dataset2 <- iris_dataset +publisher(iris_dataset2) <- "American Iris Society" + +test_that("publisher() <- assignment works", { + expect_equal(publisher(iris_dataset2), "American Iris Society") + expect_message(publisher(iris_dataset2, overwrite=FALSE) <- "Overwritten") }) publisher(iris_dataset) <- NULL test_that("publisher() works", { - expect_equal(publisher(iris_dataset), NA_character_) + expect_equal(publisher(iris_dataset), ":tba") }) test_that("publisher(x) <- c(1:2) throws and error", {