Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #136

Merged
merged 7 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ URL: https://iqss.github.io/dataverse-client-r/, https://dataverse.org/, https:/
BugReports: https://github.com/iqss/dataverse-client-r/issues
VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Generated by roxygen2: do not edit by hand

S3method(create_zip,character)
S3method(create_zip,data.frame)
S3method(create_zip,list)
S3method(dataset_id,character)
S3method(dataset_id,dataverse_dataset)
S3method(dataset_id,default)
S3method(dataverse_id,character)
S3method(dataverse_id,dataverse)
S3method(get_fileid,character)
S3method(get_fileid,dataverse_file)
S3method(get_fileid,numeric)
S3method(print,dataset_atom)
S3method(print,dataset_statement)
S3method(print,dataverse)
Expand Down
3 changes: 3 additions & 0 deletions R/SWORD_files.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
create_zip <- function(x, ...) {
UseMethod("create_zip", x)
}
#' @export
create_zip.character <- function(x, ...) {
f <- file.exists(x)
if (any(!f)) {
Expand All @@ -11,6 +12,7 @@ create_zip.character <- function(x, ...) {
return(tmp)
}
}
#' @export
create_zip.data.frame <- function(x, ...) {
tmpdf <- tempfile(fileext = ".zip")
on.exit(file.remove(tmpdf), add = TRUE)
Expand All @@ -19,6 +21,7 @@ create_zip.data.frame <- function(x, ...) {
stopifnot(!utils::zip(tmp, tmpdf))
return(tmp)
}
#' @export
create_zip.list <- function(x, ...) {
tmpdf <- sapply(seq_along(x), tempfile(fileext = ".zip"))
on.exit(file.remove(tmpdf), add = TRUE)
Expand Down
3 changes: 1 addition & 2 deletions R/dataverse-package.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#' @docType package
#' @name dataverse
#' @title Client for Dataverse Repositories
#' @description Provides access to Dataverse 4+ APIs, enabling data search, retrieval, and deposit.
Expand Down Expand Up @@ -27,4 +26,4 @@
#'
#' \href{https://dataverse.harvard.edu/}{Harvard IQSS Dataverse}
#'
NULL
"_PACKAGE"
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ dataverse_id <- function(x, ...) {
dataverse_id.default <- function(x, ...) {
x
}
#' @export
dataverse_id.character <- function(x, ...) {
get_dataverse(x, ..., check = FALSE)$id
}
#' @export
dataverse_id.dataverse <- function(x, ...) {
x$id
}
Expand All @@ -16,9 +18,11 @@ dataverse_id.dataverse <- function(x, ...) {
dataset_id <- function(x, ...) {
UseMethod("dataset_id", x)
}
#' @export
dataset_id.default <- function(x, ...) {
x
}
#' @export
dataset_id.character <- function(x, key = Sys.getenv("DATAVERSE_KEY"), server = Sys.getenv("DATAVERSE_SERVER"), ...) {
x <- prepend_doi(x)
u <- paste0(api_url(server), "datasets/:persistentId?persistentId=", x)
Expand All @@ -28,6 +32,7 @@ dataset_id.character <- function(x, key = Sys.getenv("DATAVERSE_KEY"), server =
})
jsonlite::fromJSON(httr::content(r, as = "text", encoding = "UTF-8"))[["data"]][["id"]]
}
#' @export
dataset_id.dataverse_dataset <- function(x, ...) {
x$id
}
Expand All @@ -37,6 +42,7 @@ get_fileid <- function(x, ...) {
UseMethod("get_fileid", x)
}

#' @export
get_fileid.numeric <- function(x, file, key = Sys.getenv("DATAVERSE_KEY"), server = Sys.getenv("DATAVERSE_SERVER"), ...) {
files <- dataset_files(x, key = key, server = server, ...)
ids <- unlist(lapply(files, function(x) x[["datafile"]][["id"]]))
Expand All @@ -57,6 +63,7 @@ get_fileid.numeric <- function(x, file, key = Sys.getenv("DATAVERSE_KEY"), serve
id
}

#' @export
get_fileid.character <- function(x, file, key = Sys.getenv("DATAVERSE_KEY"), server = Sys.getenv("DATAVERSE_SERVER"), ...) {
files <- dataset_files(prepend_doi(x), key = key, server = server, ...)
ids <- unlist(lapply(files, function(x) x[["dataFile"]][["id"]]))
Expand All @@ -77,6 +84,7 @@ get_fileid.character <- function(x, file, key = Sys.getenv("DATAVERSE_KEY"), ser
id
}

#' @export
get_fileid.dataverse_file <- function(x, ...) {
x[["dataFile"]][["id"]]
}
Expand Down
30 changes: 30 additions & 0 deletions man/dataverse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion tests/testthat/tests-dataset_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ test_that("check versions format", {
ds_index <- which(sapply(contents, function(x) x$identifier) == "FK2/HXJVJU")
actual <- dataset_versions(contents[[ds_index]])

expect_length(actual[[1]], 17L)
mtmorgan marked this conversation as resolved.
Show resolved Hide resolved
expected_names <- c(
"citationDate", "createTime", "datasetId", "datasetPersistentId",
"fileAccessRequest", "files", "id", "lastUpdateTime", "latestVersionPublishingState",
"license", "metadataBlocks", "publicationDate", "releaseTime",
"storageIdentifier", "UNF", "versionMinorNumber", "versionNumber",
"versionState")
expect_setequal(names(actual[[1]]), expected_names)
expect_s3_class(actual[[2]], "dataverse_dataset_version")
})
12 changes: 9 additions & 3 deletions tests/testthat/tests-get_dataframe-dataframe-basketball.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ test_that("roster-by-name", {
actual <-
get_dataframe_by_name(
filename = expected_ds$roster$label , # A value like "roster-bulls-1996.tab",
dataset = dirname(expected_ds$roster$dataFile$persistentId)#, # A value like "doi:10.70122/FK2/HXJVJU",
dataset = dirname(expected_ds$roster$dataFile$persistentId),#, # A value like "doi:10.70122/FK2/HXJVJU",
# quieten readr::read_tsv during test
.f = function(...) readr::read_tsv(..., show_col_types = FALSE)
)

expect_equal(actual, expected_file)
Expand All @@ -24,7 +26,9 @@ test_that("roster-by-doi", {

actual <-
get_dataframe_by_doi(
filedoi = expected_ds$roster$dataFile$persistentId # A value like "doi:10.70122/FK2/HXJVJU/SA3Z2V",
filedoi = expected_ds$roster$dataFile$persistentId, # A value like "doi:10.70122/FK2/HXJVJU/SA3Z2V",
# quieten readr::read_tsv during test
.f = function(...) readr::read_tsv(..., show_col_types = FALSE)
)

expect_equal(actual, expected_file)
Expand All @@ -38,7 +42,9 @@ test_that("roster-by-id", {

actual <-
get_dataframe_by_id(
fileid = expected_ds$roster$dataFile$id # A value like 1734005
fileid = expected_ds$roster$dataFile$id, # A value like 1734005
# quieten readr::read_tsv during test
.f = function(...) readr::read_tsv(..., show_col_types = FALSE)
)

expect_equal(actual, expected_file)
Expand Down
9 changes: 8 additions & 1 deletion tests/testthat/tests-get_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ test_that("download tab from DOI and filename", {
actual <- get_dataset(contents[[ds_index]])
files <- actual$files
expected_dv <- retrieve_info_dataverse("expected-dataverse.yml")
expected_names <- c(
"citationDate", "createTime", "datasetId",
"datasetPersistentId", "fileAccessRequest", "files", "id",
"lastUpdateTime", "latestVersionPublishingState", "license",
"metadataBlocks", "publicationDate", "releaseTime",
"storageIdentifier", "UNF", "versionMinorNumber",
"versionNumber", "versionState")

expect_length(actual , 17L)
expect_setequal(names(actual) , expected_names)
expect_equal(actual$id , 182158L)
expect_equal(actual$datasetId , 1734004L)
expect_equal(actual$datasetPersistentId , "doi:10.70122/FK2/HXJVJU")
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/tests-search.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
test_that("simple search query", {
testthat::skip_on_cran()
expect_true(is.data.frame(dataverse_search("Gary King", key = "", server = "dataverse.harvard.edu")))
expect_true(is.data.frame(dataverse_search("Gary King", key = "", server = "dataverse.harvard.edu", verbose = FALSE)))
})

test_that("named argument search", {
testthat::skip_on_cran()
expect_true(is.data.frame(dataverse_search(author = "Gary King", title = "Ecological Inference", key = "", server = "dataverse.harvard.edu")))
expect_true(is.data.frame(dataverse_search(author = "Gary King", title = "Ecological Inference", key = "", server = "dataverse.harvard.edu", verbose = FALSE)))
})

test_that("simple search w/type argument", {
testthat::skip_on_cran()
expect_true(is.data.frame(dataverse_search(author = "Gary King", type = "dataset", key = "", server = "dataverse.harvard.edu")))
expect_true(is.data.frame(dataverse_search(author = "Gary King", type = "dataset", key = "", server = "dataverse.harvard.edu", verbose = FALSE)))
})

test_that("date range search using fq", {
testthat::skip_on_cran()
expect_true(is.data.frame(dataverse_search("*", fq = "dateSort:[2018-01-01T00:00:00Z+TO+2019-01-01T00:00:00Z]", type = "dataset", key = "", server = "dataverse.harvard.edu")))
expect_true(is.data.frame(dataverse_search("*", fq = "dateSort:[2018-01-01T00:00:00Z+TO+2019-01-01T00:00:00Z]", type = "dataset", key = "", server = "dataverse.harvard.edu", verbose = FALSE)))
})

test_that("publication year using fq", {
testthat::skip_on_cran()
expect_true(is.data.frame(dataverse_search("*", fq = "publicationDate:2018", type = "dataset", key = "", server = "dataverse.harvard.edu")))
expect_true(is.data.frame(dataverse_search("*", fq = "publicationDate:2018", type = "dataset", key = "", server = "dataverse.harvard.edu", verbose = FALSE)))
})

test_that("filter dataverses by subject using fq", {
testthat::skip_on_cran()
expect_true(is.data.frame(dataverse_search("*", fq = "subject_ss:Social+Sciences", type = "dataverse", key = "", server = "dataverse.harvard.edu")))
expect_true(is.data.frame(dataverse_search("*", fq = "subject_ss:Social+Sciences", type = "dataverse", key = "", server = "dataverse.harvard.edu", verbose = FALSE)))
})

test_that("empty fq search", {
testthat::skip_on_cran()
expect_length(dataverse_search("*", fq = "dateSort:[2019-02-01T00:00:00Z+TO+2019-01-01T00:00:00Z]", type = "dataset", key = "", server = "dataverse.harvard.edu"), 0)
expect_length(dataverse_search("*", fq = "dateSort:[2019-02-01T00:00:00Z+TO+2019-01-01T00:00:00Z]", type = "dataset", key = "", server = "dataverse.harvard.edu", verbose = FALSE), 0)
})