-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from IQSS/dev
Dev
- Loading branch information
Showing
17 changed files
with
368 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,107 @@ | ||
#' @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 | ||
#' @template dots | ||
#' @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 | ||
get_dataset <- function(dataset, version = ":latest", key = Sys.getenv("DATAVERSE_KEY"), server = Sys.getenv("DATAVERSE_SERVER"), ...) { | ||
dataset <- dataset_id(dataset, key = key, server = server, ...) | ||
if (!is.null(version)) { | ||
u <- paste0(api_url(server), "datasets/", dataset, "/versions/", version) | ||
} else { | ||
u <- paste0(api_url(server), "datasets/", dataset) | ||
} | ||
r <- httr::GET(u, httr::add_headers("X-Dataverse-key" = key), ...) | ||
httr::stop_for_status(r) | ||
parse_dataset(httr::content(r, as = "text", encoding = "UTF-8")) | ||
get_dataset <- function( | ||
dataset, | ||
version = ":latest", | ||
key = Sys.getenv("DATAVERSE_KEY"), | ||
server = Sys.getenv("DATAVERSE_SERVER"), | ||
... | ||
) { | ||
dataset <- dataset_id(dataset, key = key, server = server, ...) | ||
if (!is.null(version)) { | ||
u <- paste0(api_url(server), "datasets/", dataset, "/versions/", version) | ||
} else { | ||
u <- paste0(api_url(server), "datasets/", dataset) | ||
} | ||
r <- httr::GET(u, httr::add_headers("X-Dataverse-key" = key), ...) | ||
httr::stop_for_status(r) | ||
parse_dataset(httr::content(r, as = "text", encoding = "UTF-8")) | ||
} | ||
|
||
#' @rdname get_dataset | ||
#' @param block A character string specifying a metadata block to retrieve. By default this is \dQuote{citation}. Other values may be available, depending on the dataset, such as \dQuote{geospatial} or \dQuote{socialscience}. | ||
#' @importFrom utils str | ||
#' @export | ||
dataset_metadata <- function(dataset, version = ":latest", block = "citation", key = Sys.getenv("DATAVERSE_KEY"), server = Sys.getenv("DATAVERSE_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 { | ||
u <- paste0(api_url(server), "datasets/", dataset, "/versions/", version, "/metadata") | ||
} | ||
dataset_metadata <- function( | ||
dataset, | ||
version = ":latest", | ||
block = "citation", | ||
key = Sys.getenv("DATAVERSE_KEY"), | ||
server = Sys.getenv("DATAVERSE_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 { | ||
u <- paste0(api_url(server), "datasets/", dataset, "/versions/", version, "/metadata") | ||
} | ||
|
||
r <- httr::GET(u, httr::add_headers("X-Dataverse-key" = key), ...) | ||
httr::stop_for_status(r) | ||
out <- httr::content(r, as = "text", encoding = "UTF-8") | ||
jsonlite::fromJSON(out)[["data"]] | ||
r <- httr::GET(u, httr::add_headers("X-Dataverse-key" = key), ...) | ||
httr::stop_for_status(r) | ||
out <- httr::content(r, as = "text", encoding = "UTF-8") | ||
jsonlite::fromJSON(out)[["data"]] | ||
} | ||
|
||
#' @rdname get_dataset | ||
#' @export | ||
dataset_files <- function(dataset, version = ":latest", key = Sys.getenv("DATAVERSE_KEY"), server = Sys.getenv("DATAVERSE_SERVER"), ...) { | ||
dataset <- dataset_id(dataset, key = key, server = server, ...) | ||
u <- paste0(api_url(server), "datasets/", dataset, "/versions/", version, "/files") | ||
r <- httr::GET(u, httr::add_headers("X-Dataverse-key" = key), ...) | ||
httr::stop_for_status(r) | ||
out <- jsonlite::fromJSON(httr::content(r, as = "text", encoding = "UTF-8"), simplifyDataFrame = FALSE)$data | ||
structure(lapply(out, `class<-`, "dataverse_file")) | ||
dataset_files <- function( | ||
dataset, | ||
version = ":latest", | ||
key = Sys.getenv("DATAVERSE_KEY"), | ||
server = Sys.getenv("DATAVERSE_SERVER"), | ||
... | ||
) { | ||
dataset <- dataset_id(dataset, key = key, server = server, ...) | ||
u <- paste0(api_url(server), "datasets/", dataset, "/versions/", version, "/files") | ||
r <- httr::GET(u, httr::add_headers("X-Dataverse-key" = key), ...) | ||
httr::stop_for_status(r) | ||
out <- jsonlite::fromJSON(httr::content(r, as = "text", encoding = "UTF-8"), simplifyDataFrame = FALSE)$data | ||
structure(lapply(out, `class<-`, "dataverse_file")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.