Skip to content

Commit

Permalink
publisher()
Browse files Browse the repository at this point in the history
  • Loading branch information
antaldaniel committed Nov 23, 2023
1 parent d6d99d6 commit 5adea31
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 19 deletions.
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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<-")
Expand All @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions R/dataset_bibentry.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
31 changes: 24 additions & 7 deletions R/publisher.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,46 @@
#' @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)
}

if (length(value)>1) {
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
}
16 changes: 15 additions & 1 deletion tests/testthat/test-dataset_bibentry.R
Original file line number Diff line number Diff line change
Expand Up @@ -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},")
})

26 changes: 21 additions & 5 deletions tests/testthat/test-publisher.R
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down

0 comments on commit 5adea31

Please sign in to comment.