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

Update contact_matrix to only accept survey() objects #107

Merged
merged 8 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Imports:
lubridate,
oai,
wpp2017,
xml2
xml2,
lifecycle
Suggests:
ggplot2,
here,
Expand All @@ -45,7 +46,7 @@ VignetteBuilder:
Encoding: UTF-8
LazyData: true
NeedsCompilation: no
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
URL: https://github.com/epiforecasts/socialmixr,
https://epiforecasts.io/socialmixr/
BugReports: https://github.com/epiforecasts/socialmixr/issues
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ importFrom(httr,status_code)
importFrom(httr,user_agent)
importFrom(jsonlite,fromJSON)
importFrom(jsonlite,toJSON)
importFrom(lifecycle,deprecate_warn)
importFrom(lubridate,period)
importFrom(lubridate,period_to_seconds)
importFrom(lubridate,years)
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# socialmixr 0.3.1.9000

* An error in `list_surveys()` was fixed which stopped this working
* `contact_matrix()` was updated to only accept `survey` objects, not DOIs and matches the documentation. It is still possible to get a contact matrix from a DOI but it is necessary to go through the `get_survey()` function.

```r
# No longer works!
contact_matrix("10.5281/zenodo.1095664")

# Recommended workflow
get_survey("10.5281/zenodo.1095664") |>
contact_matrix()
```

# socialmixr 0.3.1

Expand Down
18 changes: 16 additions & 2 deletions R/contact_matrix.r
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#' @importFrom stats xtabs runif median
#' @importFrom utils data globalVariables
#' @importFrom countrycode countrycode
#' @importFrom lifecycle deprecate_warn
#' @import data.table
#' @export
#' @autoglobal
Expand All @@ -56,8 +57,21 @@ contact_matrix <- function(survey, countries = NULL, survey.pop, age.limits, fil
missing.participant.age <- match.arg(missing.participant.age)
missing.contact.age <- match.arg(missing.contact.age)

## get the survey
survey <- get_survey(survey)
error_string <-
"must be a survey object (created using `survey()` or `get_survey()`)"

if (all(is_doi(survey))) {
deprecate_warn(
"0.3.2", paste0("contact_matrix(survey = '", error_string, "')"),
details = "Passing a DOI will be removed in version 0.4.0."
)
survey <- get_survey(survey)
} else if (!inherits(survey, "survey")) {
stop(error_string)
}

## clean the survey
survey <- clean(survey)
## check and get columns
columns <- suppressMessages(check(survey, ...))

Expand Down
11 changes: 10 additions & 1 deletion R/download_survey.r
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
download_survey <- function(survey, dir = NULL, sleep = 1) {
survey <- sub("^(https?:\\/\\/(dx\\.)?doi\\.org\\/|doi:)", "", survey)
survey <- sub("#.*$", "", survey)
is.doi <- (length(survey) > 0) && all(grepl("^10.[0-9.]{4,}/[-._;()/:A-z0-9]+$", survey))
is.doi <- (length(survey) > 0) && all(is_doi(survey))
is.url <- (length(survey) > 0) && (is.doi || grepl("^https?:\\/\\/", survey))

if (is.url && length(survey) > 1) {
Expand Down Expand Up @@ -129,3 +129,12 @@ find_common_prefix <- function(vec) {

return(lcs)
}

##' Checks if a character string is a DOI
##'
##' @param x Character vector; the string or strings to check
##' @return Logical; \code{TRUE} if \code{x} is a DOI, \code{FALSE} otherwise
##' @author Sebastian Funk
is_doi <- function(x) {
is.character(x) & grepl("^10.[0-9.]{4,}/[-._;()/:A-z0-9]+$", x)
}
20 changes: 20 additions & 0 deletions man/is_doi.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/test-matrix.r
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test_that("demography is numeric", {
})

test_that("survey argument is validated", {
expect_error(contact_matrix(survey = "bogus"), "URL")
expect_error(contact_matrix(survey = "bogus"), "survey")
})

test_that("error is thrown if no survey population can be generated", {
Expand Down Expand Up @@ -544,3 +544,9 @@ test_that("Contact matrices per capita are also generated when bootstrapping", {
), 2)
})
})

test_that("passing a DOI for survey is deprecated", {
lifecycle::expect_deprecated(
contact_matrix(survey = "10.5281/zenodo.1095664") # nolint
)
})
Loading