From 2ed1a1161d0263d87d68b2f2f2888ab563976165 Mon Sep 17 00:00:00 2001 From: Will Beasley Date: Fri, 20 Mar 2020 01:44:10 -0500 Subject: [PATCH] `data_files()`, `dataset_metadata()`, and `get_dataset()` simple tests & examples ref #4 --- DESCRIPTION | 5 +-- R/get_dataset.R | 45 ++++++++++++++++++++----- man/get_dataset.Rd | 40 +++++++++++++++++----- tests/testthat/tests-dataset_files.R | 14 ++++++++ tests/testthat/tests-dataset_metadata.R | 12 +++++++ tests/testthat/tests-get_dataset.R | 33 ++++++++++++++++++ 6 files changed, 130 insertions(+), 19 deletions(-) create mode 100644 tests/testthat/tests-dataset_files.R create mode 100644 tests/testthat/tests-dataset_metadata.R create mode 100644 tests/testthat/tests-get_dataset.R diff --git a/DESCRIPTION b/DESCRIPTION index 7454b17..492a3c2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: dataverse -Version: 0.2.1.9001 +Version: 0.2.1.9002 Title: Client for Dataverse 4 Repositories Authors@R: c( person( @@ -48,6 +48,7 @@ Suggests: covr, foreign, knitr, + purrr, testthat, UNF, yaml @@ -59,4 +60,4 @@ URL: https://github.com/iqss/dataverse-client-r BugReports: https://github.com/iqss/dataverse-client-r/issues VignetteBuilder: knitr Encoding: UTF-8 -RoxygenNote: 7.0.2 +RoxygenNote: 7.1.0 diff --git a/R/get_dataset.R b/R/get_dataset.R index 9a1b4f5..039d423 100644 --- a/R/get_dataset.R +++ b/R/get_dataset.R @@ -1,7 +1,19 @@ #' @rdname get_dataset #' @title Get dataset #' @description Retrieve a Dataverse dataset or its metadata -#' @details \code{get_dataset} retrieves details about a Dataverse dataset. \code{dataset_metadata} returns a named metadata block for a dataset. This is already returned by \code{\link{get_dataset}}, but this function allows you to retrieve just a specific block of metadata, such as citation information. \code{dataset_files} returns a list of files in a dataset, similar to \code{\link{get_dataset}}. The difference is that this returns only a list of \dQuote{dataverse_dataset} objects, whereas \code{\link{get_dataset}} returns metadata and a data.frame of files (rather than a list of file objects). +#' +#' @details +#' \code{get_dataset} retrieves details about a Dataverse dataset. +#' +#' \code{dataset_metadata} returns a named metadata block for a dataset. +#' This is already returned by \code{\link{get_dataset}}, but this function allows +#' you to retrieve just a specific block of metadata, such as citation information. +#' +#' \code{dataset_files} returns a list of files in a dataset, similar to +#' \code{\link{get_dataset}}. The difference is that this returns only a list of +#' \dQuote{dataverse_dataset} objects, whereas \code{\link{get_dataset}} returns +#' metadata and a data.frame of files (rather than a list of file objects). +#' #' @template ds #' @template version #' @template envvars @@ -9,13 +21,28 @@ #' @return A list of class \dQuote{dataverse_dataset} or a list of a form dependent on the specific metadata block retrieved. \code{dataset_files} returns a list of objects of class \dQuote{dataverse_file}. #' @examples #' \dontrun{ -#' # download file from: -#' # https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/ARKOTI -#' monogan <- get_dataverse("monogan") -#' monogan_data <- dataverse_contents(monogan) -#' d1 <- get_dataset(monogan_data[[1]]) -#' dataset_files(monogan_data[[1]]) -#' f <- get_file(d1$files$datafile$id[3]) +#' Sys.setenv("DATAVERSE_SERVER" = "demo.dataverse.org") +#' Sys.setenv("DATAVERSE_KEY" = "c7208dd2-6ec5-469a-bec5-f57e164888d4") +#' +#' # Download file from: https://demo.dataverse.org/file.xhtml?fileId=769385 +#' dv <- get_dataverse("dataverse-client-r") +#' contents <- dataverse_contents(dv) +#' +#' dataset_files(contents[[1]]) # Dataset contains 2 files +#' dataset_metadata(contents[[1]]) # Easier to query later +#' +#' set <- get_dataset(contents[[1]]) # 1st dataset w/n dataverse +#' f <- get_file(set$files$id[2]) # 2nd file w/n dataset +#' +#' # Check the *binary* representation of the file. +#' length(f) +#' head(f) +#' +#' # Examine the plain-text representation. +#' tmp <- tempfile(fileext = "svg") +#' writeBin(as.vector(f), tmp) +#' svg_lines <- readLines(tmp) +#' head(svg_lines) #' } #' @seealso \code{\link{create_dataset}}, \code{\link{update_dataset}}, \code{\link{delete_dataset}}, \code{\link{publish_dataset}}, \code{\link{dataset_files}}, \code{\link{dataset_metadata}} #' @export @@ -49,7 +76,7 @@ dataset_metadata <- function( server = Sys.getenv("DATAVERSE_SERVER"), ... ) { -dataset <- dataset_id(dataset, key = key, server = server, ...) + dataset <- dataset_id(dataset, key = key, server = server, ...) if (!is.null(block)) { u <- paste0(api_url(server), "datasets/", dataset, "/versions/", version, "/metadata/", block) } else { diff --git a/man/get_dataset.Rd b/man/get_dataset.Rd index ac656ef..70175c5 100644 --- a/man/get_dataset.Rd +++ b/man/get_dataset.Rd @@ -51,17 +51,41 @@ A list of class \dQuote{dataverse_dataset} or a list of a form dependent on the Retrieve a Dataverse dataset or its metadata } \details{ -\code{get_dataset} retrieves details about a Dataverse dataset. \code{dataset_metadata} returns a named metadata block for a dataset. This is already returned by \code{\link{get_dataset}}, but this function allows you to retrieve just a specific block of metadata, such as citation information. \code{dataset_files} returns a list of files in a dataset, similar to \code{\link{get_dataset}}. The difference is that this returns only a list of \dQuote{dataverse_dataset} objects, whereas \code{\link{get_dataset}} returns metadata and a data.frame of files (rather than a list of file objects). +\code{get_dataset} retrieves details about a Dataverse dataset. + +\code{dataset_metadata} returns a named metadata block for a dataset. +This is already returned by \code{\link{get_dataset}}, but this function allows +you to retrieve just a specific block of metadata, such as citation information. + +\code{dataset_files} returns a list of files in a dataset, similar to +\code{\link{get_dataset}}. The difference is that this returns only a list of +\dQuote{dataverse_dataset} objects, whereas \code{\link{get_dataset}} returns +metadata and a data.frame of files (rather than a list of file objects). } \examples{ \dontrun{ -# download file from: -# https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/ARKOTI -monogan <- get_dataverse("monogan") -monogan_data <- dataverse_contents(monogan) -d1 <- get_dataset(monogan_data[[1]]) -dataset_files(monogan_data[[1]]) -f <- get_file(d1$files$datafile$id[3]) +Sys.setenv("DATAVERSE_SERVER" = "demo.dataverse.org") +Sys.setenv("DATAVERSE_KEY" = "c7208dd2-6ec5-469a-bec5-f57e164888d4") + +# Download file from: https://demo.dataverse.org/file.xhtml?fileId=769385 +dv <- get_dataverse("dataverse-client-r") +contents <- dataverse_contents(dv) + +dataset_files(contents[[1]]) # Dataset contains 2 files +dataset_metadata(contents[[1]]) # Easier to query later + +set <- get_dataset(contents[[1]]) # 1st dataset w/n dataverse +f <- get_file(set$files$id[2]) # 2nd file w/n dataset + +# Check the *binary* representation of the file. +length(f) +head(f) + +# Examine the plain-text representation. +tmp <- tempfile(fileext = "svg") +writeBin(as.vector(f), tmp) +svg_lines <- readLines(tmp) +head(svg_lines) } } \seealso{ diff --git a/tests/testthat/tests-dataset_files.R b/tests/testthat/tests-dataset_files.R new file mode 100644 index 0000000..37df7b8 --- /dev/null +++ b/tests/testthat/tests-dataset_files.R @@ -0,0 +1,14 @@ +# See https://demo.dataverse.org/dataverse/dataverse-client-r +# https://doi.org/10.70122/FK2/FAN622 + +test_that("download tab from DOI and filename", { + dv <- get_dataverse("dataverse-client-r") + contents <- dataverse_contents(dv) + actual <- dataset_files(contents[[1]]) + + expect_length(actual, 2L) + expect_equal(purrr::map_chr(actual, "label") , c("roster-bulls-1996.tab", "vector-basketball.svg")) + expect_equal(purrr::map_lgl(actual, "restricted") , c(FALSE, FALSE)) + expect_equal(purrr::map_int(actual, "version") , c(2L, 2L)) + expect_equal(purrr::map_int(actual, "datasetVersionId") , c(53155L, 53155L)) +}) diff --git a/tests/testthat/tests-dataset_metadata.R b/tests/testthat/tests-dataset_metadata.R new file mode 100644 index 0000000..dedd37a --- /dev/null +++ b/tests/testthat/tests-dataset_metadata.R @@ -0,0 +1,12 @@ +# See https://demo.dataverse.org/dataverse/dataverse-client-r +# https://doi.org/10.70122/FK2/FAN622 + +test_that("download tab from DOI and filename", { + dv <- get_dataverse("dataverse-client-r") + contents <- dataverse_contents(dv) + actual <- dataset_metadata(contents[[1]]) + + expect_length(actual, 2L) + expect_equal(actual[[1]], "Citation Metadata") + expect_s3_class(actual[[2]], "data.frame") +}) diff --git a/tests/testthat/tests-get_dataset.R b/tests/testthat/tests-get_dataset.R new file mode 100644 index 0000000..ed8bf36 --- /dev/null +++ b/tests/testthat/tests-get_dataset.R @@ -0,0 +1,33 @@ +# See https://demo.dataverse.org/dataverse/dataverse-client-r +# https://doi.org/10.70122/FK2/FAN622 + +test_that("download tab from DOI and filename", { + dv <- get_dataverse("dataverse-client-r") + contents <- dataverse_contents(dv) + actual <- get_dataset(contents[[1]]) + files <- actual$files + + expect_length(actual , 13L) + expect_equal(actual$id , 53155L) + expect_equal(actual$datasetId , 396356L) + expect_equal(actual$datasetPersistentId , "doi:10.70122/FK2/FAN622") + expect_equal(actual$storageIdentifier , "file://10.70122/FK2/FAN622") + expect_equal(actual$versionState , "DRAFT") + expect_equal(actual$UNF , "UNF:6:hrleySyT6vzwEih3+nhp8A==") + expect_equal(actual$license , "CC0") + + expect_equal(nrow(files) , 2L) + expect_equal(ncol(files) , 21L) + + expect_equal(files$label , c("roster-bulls-1996.tab", "vector-basketball.svg")) + expect_equal(files$restricted , c(FALSE, FALSE)) + expect_equal(files$version , c(2L, 2L)) + expect_equal(files$datasetVersionId , c(53155L, 53155L)) + expect_equal(files$directoryLabel , c(NA, "resources")) + expect_equal(files$id , c(396357L, 769385L)) + expect_equal(files$persistentId , c("", "")) + expect_equal(files$pidURL , c("", "")) + expect_equal(files$filename , c("roster-bulls-1996.tab", "vector-basketball.svg")) + expect_equal(files$description , c(NA, "CC-0-from-https://publicdomainvectors.org/en/free-clipart/Basketball-vector-symbol/69448.html")) + +})