Skip to content

Commit

Permalink
2-space indent; parameters on separate lines
Browse files Browse the repository at this point in the history
ref #41
  • Loading branch information
wibeasley committed Feb 24, 2020
1 parent ff7e87c commit 5ec4d7f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 46 deletions.
75 changes: 47 additions & 28 deletions R/get_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,62 @@
#' }
#' @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"))
}
38 changes: 20 additions & 18 deletions R/get_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@
#' }
#' @importFrom utils unzip
#' @export
get_file <-
function(file,
dataset = NULL,
format = c("original", "RData", "prep", "bundle"),
# thumb = TRUE,
vars = NULL,
key = Sys.getenv("DATAVERSE_KEY"),
server = Sys.getenv("DATAVERSE_SERVER"),
...) {
get_file <- function(
file,
dataset = NULL,
format = c("original", "RData", "prep", "bundle"),
# thumb = TRUE,
vars = NULL,
key = Sys.getenv("DATAVERSE_KEY"),
server = Sys.getenv("DATAVERSE_SERVER"),
...
) {
format <- match.arg(format)

# single file ID
if (is.numeric(file))
fileid <- file
Expand All @@ -74,7 +75,7 @@ get_file <-
} else {
fileid <- file
}


# # request multiple files -----
# if (length(fileid) > 1) {
Expand Down Expand Up @@ -142,13 +143,14 @@ get_file_name_from_header <- function(x) {
#' @rdname files
#' @import xml2
#' @export
get_file_metadata <-
function(file,
dataset = NULL,
format = c("ddi", "preprocessed"),
key = Sys.getenv("DATAVERSE_KEY"),
server = Sys.getenv("DATAVERSE_SERVER"),
...) {
get_file_metadata <- function(
file,
dataset = NULL,
format = c("ddi", "preprocessed"),
key = Sys.getenv("DATAVERSE_KEY"),
server = Sys.getenv("DATAVERSE_SERVER"),
...
) {
# get file ID from doi
if (!is.numeric(file)) {
if (inherits(file, "dataverse_file")) {
Expand Down

0 comments on commit 5ec4d7f

Please sign in to comment.