Skip to content

Commit

Permalink
change handling of dataverse version issue (#72)
Browse files Browse the repository at this point in the history
instead of a warning in .onLoad, issue a startup message if the package version is too low, and don't throw an error unless download_file() is actually reached
  • Loading branch information
andybega committed Oct 20, 2020
1 parent dc6c821 commit 9731e23
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
14 changes: 14 additions & 0 deletions R/dataverse.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Stuff that touches dataverse

check_dataverse_version <- function() {
# Check that the installed dataverse version has the bug fix references in
# #51, #58, and lastly #72
if (utils::packageVersion("dataverse") < "0.2.1.9001") {
msg <- paste0(c(
strwrap("There is bug in the dataverse R package prior to version 0.2.1.9001 that breaks file downloading. Please check on CRAN if a newer version is available or install the development version from GitHub using:"),
" remotes::install_github(\"IQSS/dataverse-client-r\")"
), collapse = "\n")
return(msg)
}
invisible(TRUE)
}
13 changes: 4 additions & 9 deletions R/download.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#' @md
download_file <- function(file, to_dir, repo = "historic", file_id = NULL, new_name = NULL) {

# see #72
msg <- check_dataverse_version()
if (!isTRUE(msg)) stop(msg)

if (length(file) > 1) stop("I'm not vectorized")

# override default repo if certain filename is detected
Expand All @@ -40,15 +44,6 @@ download_file <- function(file, to_dir, repo = "historic", file_id = NULL, new_n
# version from github. See #51 and #58.
f <- dataverse::get_file(file = file_ref, dataset = get_doi()[[repo]],
format = NULL)
# this is a manual workaround in case get_file is still broken. It will cause
# R check warnings and notes, and thus breaks Travis-CI.
# if (is.null(file_id)) stop("temp workaround for #58 needs integer file ID")
# key = Sys.getenv("DATAVERSE_KEY")
# server = Sys.getenv("DATAVERSE_SERVER")
# u <- paste0(dataverse:::api_url(server), "access/datafile/", file_id)
# r <- httr::GET(u, httr::add_headers(`X-Dataverse-key` = key))
# httr::stop_for_status(r)
# f <- httr::content(r, as = "raw")

# Decide how to handle based on whether extraction is needed
if (tools::file_ext(file)=="zip") {
Expand Down
16 changes: 5 additions & 11 deletions R/icews-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,24 @@ globalVariables(c("cameo_codes", "goldstein_mappings"))
options("icews.debug" = FALSE)
}

# Check that the installed dataverse version has the bug fix references in
# #51.
if (utils::packageVersion("dataverse") < "0.2.1.9001") {
msg <- paste0(c(
strwrap("There is bug in the dataverse R package prior to version 0.2.1.9001 that breaks file downloading. Please check on CRAN if a newer version is available or install the development version from GitHub using:"),
" remotes::install_github(\"IQSS/dataverse-client-r\")"
), collapse = "\n")
warning(msg, call. = FALSE)
}

# Make sure the DATAVERSE_SERVER env variable is set
if (Sys.getenv("DATAVERSE_SERVER")!="dataverse.harvard.edu") {
msg <- paste0(c(
strwrap("The R dataverse client requires the DATAVERSE_SERVER environment variable to be set. See <https://github.com/IQSS/dataverse-client-r>. Please run:"),
"Sys.setenv(DATAVERSE_SERVER = \"dataverse.harvard.edu\")"
), collapse = "\n")
warning(msg, call. = FALSE)
warning(msg)
}
}

.onAttach <- function(...) {
opts <- get_icews_opts()
msg <- format(opts)
packageStartupMessage(paste0(msg, collapse = "\n"))

# see #72
msg <- check_dataverse_version()
if (!isTRUE(msg)) packageStartupMessage(msg)
}

## usethis namespace: start
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-download.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ test_that("download_file works", {
# runs without error
out <- with_mock(
`dataverse::get_file` = function(...) readBin(tsv_sample_path(), "raw", 1e6),
`icews::check_dataverse_version` = function() invisible(TRUE), # dataverse version issue, see #72
download_file(file = "events.2018.sample.tab", to_dir = td)
)
# returns link to downloaded data
expect_equal(basename(out), "events.2018.sample.tab")

# complains about vector input
expect_error(
download_file(file = c("a", "b"), to_dir = td),
with_mock(
`icews::check_dataverse_version` = function() invisible(TRUE), # dataverse version issue, see #72
download_file(file = c("a", "b"), to_dir = td)
),
"I'm not vectorized"
)
})
Expand Down

0 comments on commit 9731e23

Please sign in to comment.