Skip to content

Commit

Permalink
Merge branch 'main' into fix-list-survey
Browse files Browse the repository at this point in the history
  • Loading branch information
sbfnk committed Oct 18, 2023
2 parents e75b89b + cffe534 commit 66728c2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ importFrom(utils,globalVariables)
importFrom(utils,hasName)
importFrom(utils,packageVersion)
importFrom(utils,read.csv)
importFrom(xml2,xml_attr)
importFrom(xml2,xml_find_all)
importFrom(xml2,xml_find_first)
importFrom(xml2,xml_text)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* More generous filename checks now pass files named e.g. "..._participants_common..." an not only "...participant_common..."
* The package now sets a custom user agent when downloading survey data (#82).
* A problem was fixed where attempted joins of files could lead to blowing up memeory use (#75).
* A problem was fixed where the updated Zenodo API caused downloading to fail (#91).
* A problem was fixed where the updated Zenodo API caused listing surveys to fail (#96).

# socialmixr 0.2.0
Expand Down
15 changes: 9 additions & 6 deletions R/download_survey.r
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @importFrom jsonlite fromJSON toJSON
#' @importFrom curl curl_download
#' @importFrom utils read.csv packageVersion
#' @importFrom xml2 xml_text xml_find_first
#' @importFrom xml2 xml_text xml_find_first xml_find_all xml_attr
#' @autoglobal
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -61,18 +61,21 @@ download_survey <- function(survey, dir = NULL) {
reference <- list(
title = parsed_cite$name,
bibtype = "Misc",
author = parsed_cite$creator$name,
author = parsed_cite$author$name,
year = data.table::year(parsed_cite$datePublished)
)
if ("version" %in% names(parsed_cite)) {
reference[["note"]] <- paste("Version", parsed_cite$version)
}
reference[[ifelse(is.doi, "doi", "url")]] <- survey

data <- data.table(parsed_cite$distribution)
links <- xml_attr(
xml_find_all(parsed_body, "//link[@type=\"text/csv\"]"), "href"
)

data <- data.table(url = links)
## only download csv files
data <- data[encodingFormat == "csv"]
data[, file_name := tolower(basename(contentUrl))]
data[, file_name := tolower(basename(url))]

if (anyDuplicated(data$file_name) > 0) {
warning(
Expand All @@ -95,7 +98,7 @@ download_survey <- function(survey, dir = NULL) {
write(reference_json, reference_file_path)

files <- c(reference_file_path, vapply(seq_len(nrow(data)), function(i) {
url <- data[i, ]$contentUrl
url <- data[i, ]$url
temp <- file.path(dir, data[i, ]$file_name)
message("Downloading ", url)
dl <- curl_download(url, temp)
Expand Down
2 changes: 0 additions & 2 deletions R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ utils::globalVariables(c(
"bootstrap.weight", # <contact_matrix>
"proportion", # <contact_matrix>
"participants", # <contact_matrix>
"encodingFormat", # <download_survey>
"file_name", # <download_survey>
"contentUrl", # <download_survey>
"common_doi", # <list_surveys>
"doi.nb", # <list_surveys>
"identifier.1", # <list_surveys>
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-surveys.r
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ test_that("list of surveys is not empty", {
test_that("surveys can be downloaded", {
skip_if_offline("zenodo.org")
skip_on_cran()
skip_on_ci()

s <- suppressMessages(suppressWarnings(get_survey("10.5281/zenodo.1059920"))) # nolint
s <- suppressMessages(suppressWarnings(get_survey("10.5281/zenodo.1095664"))) # nolint

expect_s3_class(s, "survey")
expect_named(
Expand All @@ -30,7 +29,7 @@ test_that("missing surveys can't be cited", {
})

test_that("multiple DOI's cannot be loaded", {
expect_error(suppressMessages(suppressWarnings(get_survey(c("10.5281/zenodo.1059920", "10.5281/zenodo.1059920"))))) # nolint
expect_error(suppressMessages(suppressWarnings(get_survey(c("10.5281/zenodo.1095664", "10.5281/zenodo.1127693"))))) # nolint
})

test_that("deprecated functions are warned about", {
Expand Down

0 comments on commit 66728c2

Please sign in to comment.