diff --git a/DESCRIPTION b/DESCRIPTION index c490d11..d45210b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,15 +28,16 @@ Imports: stringr, tibble, tidyr, - tidyverse, - urltools + urltools, + janitor Suggests: - devtools, - testthat + devtools, + testthat (>= 3.0.0) License: MIT + file LICENSE Encoding: UTF-8 LazyData: true -RoxygenNote: 7.1.2 +RoxygenNote: 7.2.3 Roxygen: list(markdown = TRUE) Depends: R (>= 2.10) +Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 500a962..947f78b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,12 +1,32 @@ # Generated by roxygen2: do not edit by hand +export(cases_from_contacts) export(check_godata_url) +export(clean_case_address_history) +export(clean_case_med_history) +export(clean_case_vax_history) +export(clean_cases) +export(clean_contact_address_history) +export(clean_contact_vax_history) +export(clean_contacts) +export(clean_contacts_of_contacts) +export(clean_contacts_of_contacts_address_history) +export(clean_contacts_of_contacts_vax_history) +export(clean_events) +export(clean_followups) +export(clean_locations) +export(clean_relationships) +export(clean_teams) +export(clean_users) +export(contacts_per_case) export(expand_location_tree) +export(exposures_per_case) export(get_access_token) export(get_active_outbreak) export(get_all_outbreaks) export(get_cases) export(get_cases_epiwindow) +export(get_cases_questionnaire) export(get_clusters) export(get_contacts) export(get_contacts_epiwindow) @@ -29,6 +49,7 @@ export(get_users) export(mongify_date) export(null2na) export(set_active_outbreak) +export(translate_categories) import(data.table) import(dplyr) import(httr) @@ -36,11 +57,8 @@ import(jsonlite) import(lubridate) import(purrr) import(stringr) -import(tibble) import(tidyr) import(urltools) importFrom(jsonlite,fromJSON) importFrom(magrittr,"%>%") importFrom(purrr,pluck) -importFrom(stringr,str_split) -importFrom(utils,read.csv) diff --git a/R/batch_downloader.R b/R/batch_downloader.R index 86e4af6..50fd60f 100644 --- a/R/batch_downloader.R +++ b/R/batch_downloader.R @@ -2,15 +2,19 @@ #' #' A housekeeping function to do batch downloads. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' @param api_call_n The API url to get the number of records. #' @param api_call_get The API url to GET the records. -#' @param batch_size Specifies the number of records to retrieve in each iteration. +#' @param batch_size Specifies the number of records to retrieve in each +#' iteration. #' #' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' `\link[tidyr]{nest}` for assitance with unnesting. #' #' @examples #' \dontrun{ @@ -19,66 +23,88 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' cases <- get_cases(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck -#' -#' -batch_downloader <- function(url = url, - username = username, - password = password, - api_call_n = api_call_n, - api_call_get = api_call_get, - batch_size = batch_size) { +batch_downloader <- function(url, + username, + password, + api_call_n, + api_call_get, + batch_size) { + + num_record_request <- httr::GET( + paste0(api_call_n), + httr::add_headers( + Authorization = paste("Bearer", get_access_token( + url = url, + username = username, + password = password + ), sep = " "))) - #get total number of records - df_n <- GET(paste0(api_call_n), - add_headers(Authorization = paste("Bearer", get_access_token(url=url, username=username, password=password), sep = " "))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - unlist() %>% - unname() + num_record_content <- httr::content(num_record_request, as = "text") + + num_records <- jsonlite::fromJSON(num_record_content, flatten = TRUE) + num_records <- num_records$count #Import records in batches - df <- tibble() - batch_size <- batch_size # number of records to import per iteration - skip <-0 + df <- tibble::tibble() + skip <- 0 message("****************************") #Download records in batches, and then append them into a single dataset - while (skip < df_n) { + while (skip < num_records) { #Progress message - if (df_n <= batch_size) message(paste0("...downloading records 1 to ",df_n)) - if (df_n > batch_size) message(paste0("...downloading records ", as.character(skip+1, scientific = FALSE), " to ", format(skip+batch_size, scientific = FALSE))) + if (num_records <= batch_size) { + message(paste0("...downloading records 1 to ", num_records)) + } + if (num_records > batch_size) { + message( + paste0( + "...downloading records ", + as.character(skip + 1, scientific = FALSE), + " to ", + format(skip + batch_size, scientific = FALSE) + ) + ) + } #fetch the batch of records - df.i <- GET(paste0(api_call_get, - "?filter={%22limit%22:",format(batch_size, scientific = FALSE),",%22skip%22:",format(skip, scientific = FALSE),"}"), - add_headers(Authorization = paste("Bearer", get_access_token(url=url, username=username, password=password), sep = " "))) %>% - content(as='text') %>% - fromJSON( flatten=TRUE) %>% - as_tibble() + record_request <- httr::GET( + paste0( + api_call_get, + "?filter={%22limit%22:", + format(batch_size, scientific = FALSE), + ",%22skip%22:", + format(skip, scientific = FALSE), + "}" + ), + httr::add_headers( + Authorization = paste("Bearer", get_access_token( + url = url, + username = username, + password = password), + sep = " ") + ) + ) + + record_content <- httr::content(record_request, as = "text") + + records <- jsonlite::fromJSON(record_content, flatten = TRUE) + + records <- tibble::as_tibble(records) #append the new batch of records to the existing data frame - df <- df %>% - bind_rows(df.i) + df <- dplyr::bind_rows(df, records) #update numbers for the next iteration skip <- skip + batch_size - rm(df.i) + records <- NULL } - rm(batch_size, skip, df_n) return(df) } - - diff --git a/R/cases_from_contacts.R b/R/cases_from_contacts.R new file mode 100644 index 0000000..7cd9fdb --- /dev/null +++ b/R/cases_from_contacts.R @@ -0,0 +1,106 @@ +#' Pull out all cases that used to be contacts +#' +#' @param cases_clean The cleaned case data. Case data is returned by +#' [`get_cases()`] and cleaned by [`clean_cases()`]. +#' +#' @return A tibble containing the cases that used to be contacts. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' # other cleaned data required for `clean_cases()` +#' cases_vacc_history_clean <- clean_case_vax_history(cases = cases) +#' cases_address_history_clean <- clean_case_address_history(cases = cases) +#' cases_dateranges_history_clean <- clean_case_med_history(cases = cases) +#' +#' cases_clean <- clean_cases( +#' cases = cases, +#' cases_address_history_clean = cases_address_history_clean, +#' cases_vacc_history_clean = cases_vacc_history_clean, +#' cases_dateranges_history_clean = cases_dateranges_history_clean +#' ) +#' +#' cases_from_contacts <- cases_from_contacts(cases_clean = cases_clean) +#' } +cases_from_contacts <- function(cases_clean) { + + contacts_becoming_cases <- dplyr::filter( + .data = cases_clean, + .data$was_contact == TRUE + ) + + # set this status to became case and no longer active + contacts_becoming_cases <- dplyr::mutate( + .data = contacts_becoming_cases, + follow_up_status = "BECAME_CASE", + was_case = NA, + date_of_last_contact = NA, + follow_up_team_id = NA, + relationship_exposure_type = NA, + relationship_context_of_transmission = NA, + relationship_exposure_duration = NA, + relationship_exposure_frequency = NA, + relationship_certainty_level = NA, + relationship_cluster_id = NA, + ) + + # organize order of vars, only bring in what we need, take away confusing vars + contacts_becoming_cases <- dplyr::select( + .data = contacts_becoming_cases, + "id", # identifier + "visual_id", # identifier + "classification", # identifier + "follow_up_status", # identifier + "first_name", # demographics + "middle_name", # demographics + "last_name", # demographics + "gender", # demographics + "age", # demographics + "age_class", # demographics + "occupation", # demographics + "pregnancy_status", # demographics + "date_of_reporting", # dates + "date_of_last_contact", # dates + "date_of_burial", # dates + "risk_level", # epi + "risk_reason", # epi + "responsible_user_id", # assigned contact tracer + "follow_up_team_id", # assigned contact tracer + dplyr::matches("^admin_.*name$"), # address + "lat", # address + "long", # address + "address", # address + "postal_code", # address + "city", # address + "telephone", # address + "email", # address + "vaccinated", + "outcome", # outcome + "date_of_outcome", # outcome + "relationship_exposure_type", + "relationship_context_of_transmission", + "relationship_exposure_duration", + "relationship_exposure_frequency", + "relationship_certainty_level", + "relationship_cluster_id", + "location_id", # uuid in case need later for joining of whatever sort + "created_by", # record modification + "datetime_created_at", # record modification + "updated_by", # record modification + "datetime_updated_at" # record modification + ) + + return(contacts_becoming_cases) +} diff --git a/R/check_godata_url.R b/R/check_godata_url.R index 8a87f79..1196961 100644 --- a/R/check_godata_url.R +++ b/R/check_godata_url.R @@ -4,28 +4,28 @@ #' is valid. This is a housekeeping function #' used in many of the other `godataR` functions. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! +#' @param success_code A numeric specifying which code is returned by the API +#' when successfully returning the status code. Default is 200. #' #' @return #' Boolean, where `TRUE` indicates a valid URL. #' @examples #' \dontrun{ #' url <- "https://MyGoDataServer.com/" -#' check_godata_url(url=url) +#' check_godata_url(url = url) #' } -#' @importFrom magrittr %>% -#' @import httr -#' @importFrom purrr pluck #' @export -check_godata_url <- function(url=url) { +check_godata_url <- function(url, + success_code = 200) { # Get status code for version check - status_code <- GET(paste0(url,"api/system-settings/version")) %>% - pluck("status_code") + status_code <- httr::GET(paste0(url, "api/system-settings/version")) - # create boolean based on status code being 200 (success) - check <- (status_code==200) + status_code <- purrr::pluck(status_code, "status_code") - return(check) + # return boolean based on status code being a success + return(isTRUE(status_code == success_code)) } diff --git a/R/check_godata_version.R b/R/check_godata_version.R index 8557dae..d59f964 100644 --- a/R/check_godata_version.R +++ b/R/check_godata_version.R @@ -5,43 +5,42 @@ #' This is a housekeeping function used in #' many of the other `godataR` functions. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' #' @return Boolean, where `TRUE` indicates version 2.38.1 or later. #' @examples #' \dontrun{ #' url <- "https://MyGoDataServer.com/" -#' check_godata_version(url=url) +#' check_godata_version(url = url) #' } -#' @importFrom magrittr %>% -#' @importFrom stringr str_split -check_godata_version <- function(url=url) { +check_godata_version <- function(url = url) { # Get Current Version of Go.Data - gd.version <- get_godata_version(url=url) + gd_version <- get_godata_version(url = url) # Convert string to vector of 3 numbers - gd.version <- str_split(gd.version, "[.]") %>% - unlist() %>% - as.numeric() + gd_version <- strsplit(x = gd_version, split = "[.]") + + gd_version <- as.numeric(unlist(gd_version)) + + stopifnot( + "godata version from API does not have major, minor and patch versioning" = + length(gd_version) == 3 + ) + + names(gd_version) <- c("major", "minor", "patch") # Check if 2.38.1 or later # Should be TRUE if it is version 2.38.1 or later & # FALSE if version 2.38.0 or earlier - if (gd.version[1] < 2) { - after.2.38.1 <- FALSE - } else if (gd.version[1]==2 & gd.version[2] < 38) { - after.2.38.1 <- FALSE - } else if (gd.version[1]==2 & gd.version[2]==38 & gd.version[3]<1) { - after.2.38.1 <- FALSE - } else if (gd.version[1]==2 & gd.version[2]==38 & gd.version[3]>=1) { - after.2.38.1 <- TRUE - } else if (gd.version[1]==2 & gd.version[2]>38) { - after.2.38.1 <- TRUE - } else if (gd.version[1]>2) { - after.2.38.1 <- TRUE + if (gd_version["major"] < 2) { + return(FALSE) + } else if (gd_version["major"] == 2 && gd_version["minor"] < 38) { + return(FALSE) + } else if (gd_version["major"] == 2 && gd_version["minor"] == 38 && gd_version["patch"] == 0) { + return(FALSE) + } else { + return(TRUE) } - - return(after.2.38.1) - } diff --git a/R/clean_case_address_history.R b/R/clean_case_address_history.R new file mode 100644 index 0000000..2be9f22 --- /dev/null +++ b/R/clean_case_address_history.R @@ -0,0 +1,123 @@ +#' Extract address information from case data +#' +#' @description This function un-nests and cleans the address data and stores +#' it in a standalone table with all addresses, even if there is more than 1 +#' per person. +#' +#' @param cases A tibble with case data. Case data is returned by +#' [`get_cases()`]. +#' @param locations_clean A tibble with cleaned locations data. Locations data +#' is returned by [`get_locations()`] and cleaned by [`clean_locations()`]. +#' @param language_tokens A tibble of language tokens returned by +#' [`get_language_tokens()`] to translate the string tokens in the data. +#' +#' @return A tibble with address information from cases data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) +#' +#' locations_clean <- clean_locations(locations = locations) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' case_address_history <- clean_case_address_history( +#' cases = cases, +#' locations_clean = locations_clean, +#' language_tokens = language_tokens +#' ) +#' } +clean_case_address_history <- function(cases, + locations_clean, + language_tokens) { + + cases_address_history_clean <- dplyr::filter( + .data = cases, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + cases_address_history_clean <- dplyr::select( + .data = cases_address_history_clean, + "id", "visualId", "addresses" + ) + + cases_address_history_clean <- tidyr::unnest( + data = cases_address_history_clean, + cols = "addresses", + names_sep = "_") + + cases_address_history_clean <- dplyr::select_all( + .tbl = cases_address_history_clean, + .funs = ~gsub("\\.", "_", tolower(.)) + ) + + cases_address_history_clean <- dplyr::select_if( + .tbl = cases_address_history_clean, + purrr::negate(is.list) + ) + + cases_address_history_clean <- translate_categories( + data = cases_address_history_clean, + language_tokens = language_tokens + ) + + cases_address_history_clean <- dplyr::left_join( + cases_address_history_clean, + locations_clean, + by = c("addresses_locationid" = "location_id") + ) + + # bring in GPS from locations in case blank from case record, otherwise use + # case + cases_address_history_clean <- dplyr::mutate( + .data = cases_address_history_clean, + lat = dplyr::case_when( + is.na(addresses_geolocation_lat) ~ lat, + TRUE ~ addresses_geolocation_lat + ), + long = dplyr::case_when( + is.na(addresses_geolocation_lng) ~ lat, + TRUE ~ addresses_geolocation_lng + ) + ) + + cases_address_history_clean <- dplyr::select( + .data = cases_address_history_clean, + "id", + "visualid", + "addresses_locationid", + "addresses_typeid", + "lat", + "long", + address = "addresses_addressline1", + postal_code = "addresses_postalcode", + city = "addresses_city", + telephone = "addresses_phonenumber", + email = "addresses_emailaddress", + dplyr::matches("^admin_.*name$") + ) + + return(cases_address_history_clean) +} diff --git a/R/clean_case_med_history.R b/R/clean_case_med_history.R new file mode 100644 index 0000000..c9d802b --- /dev/null +++ b/R/clean_case_med_history.R @@ -0,0 +1,87 @@ +#' Extracts and cleans medical history from case data +#' +#' @description This function un-nests and cleans date ranges of isolation and +#' hospitalization history and stores it in a standalone table. +#' +#' @param cases A tibble with case data. Case data is returned by +#' [`get_cases()`]. +#' @param language_tokens A tibble of language tokens returned by +#' [`get_language_tokens()`] to translate the string tokens in the data. +#' +#' @return A tibble with information on isolation and hospitalization history. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' cases_med_history <- clean_case_med_history( +#' cases = cases, +#' language_tokens = language_tokens +#' ) +#' } +clean_case_med_history <- function(cases, + language_tokens) { + + cases_dateranges_history_clean <- dplyr::filter( + .data = cases, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # cannot unnest on mix of data frames and lists so change empty lists to empty + # data frames + cases_dateranges_history_clean$dateRanges <- purrr::map( + cases_dateranges_history_clean$dateRanges, + .f = function(x) { + if (length(x) == 0) x <- data.frame() + x + } + ) + + cases_dateranges_history_clean <- tidyr::unnest( + data = cases_dateranges_history_clean, + cols = dateRanges, + names_sep = "_" + ) + + cases_dateranges_history_clean <- dplyr::select_at( + .tbl = cases_dateranges_history_clean, + dplyr::vars(id, visualId, starts_with("dateRanges")), + tolower + ) + + cases_dateranges_history_clean <- translate_categories( + data = cases_dateranges_history_clean, + language_tokens = language_tokens + ) + + cases_dateranges_history_clean <- dplyr::mutate_at( + .tbl = cases_dateranges_history_clean, + dplyr::vars(dateranges_startdate, dateranges_enddate), + as.Date + ) + + cases_dateranges_history_clean <- dplyr::select_if( + .tbl = cases_dateranges_history_clean, + purrr::negate(is.list) + ) + + return(cases_dateranges_history_clean) +} diff --git a/R/clean_case_vax_history.R b/R/clean_case_vax_history.R new file mode 100644 index 0000000..9201fbb --- /dev/null +++ b/R/clean_case_vax_history.R @@ -0,0 +1,86 @@ +#' Cleans vaccination data from case data +#' +#' @description Cleans and un-nests vaccination history, where vaccination is +#' complete, from case data. Case data is returned from [`get_cases()`]. +#' +#' @param cases A tibble with address information from cases data. +#' @param language_tokens A tibble of language tokens returned by +#' [`get_language_tokens()`] to translate the string tokens in the data. +#' +#' @return A tibble with cleaned and un-nested vaccination history data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' vax_history <- clean_case_vax_history( +#' cases = cases, +#' language_tokens = language_tokens +#' ) +#' } +clean_case_vax_history <- function(cases, + language_tokens) { + + cases_vacc_history_clean <- dplyr::filter( + .data = cases, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # cannot unnest on mix of data frames and lists so change empty lists to empty + # data frames + cases_vacc_history_clean$vaccinesReceived <- purrr::map( + cases_vacc_history_clean$vaccinesReceived, + .f = function(x) { + if (length(x) == 0) x <- data.frame() + x + } + ) + + cases_vacc_history_clean <- tidyr::unnest( + data = cases_vacc_history_clean, + "vaccinesReceived", + names_sep = "_" + ) + + cases_vacc_history_clean <- dplyr::select( + .data = cases_vacc_history_clean, + "id", + "visualId", + dplyr::starts_with("vaccinesReceived") + ) + + cases_vacc_history_clean <- dplyr::rename_with( + .data = cases_vacc_history_clean, + .fn = tolower + ) + + cases_vacc_history_clean <- translate_categories( + data = cases_vacc_history_clean, + language_tokens = language_tokens + ) + + cases_vacc_history_clean <- dplyr::mutate( + .data = cases_vacc_history_clean, + dplyr::across("vaccinesreceived_date", as.Date) + ) + + return(cases_vacc_history_clean) +} diff --git a/R/clean_cases.R b/R/clean_cases.R new file mode 100644 index 0000000..27932ed --- /dev/null +++ b/R/clean_cases.R @@ -0,0 +1,275 @@ +#' Cleans case data +#' +#' @description Cleans and un-nests case data. Case data is returned by +#' [`get_cases()`]. +#' +#' @param cases A `tibble` containing the case data. +#' @param locations_clean A tibble with cleaned locations data. Locations data +#' is returned by [`get_locations()`] and cleaned by [`clean_locations()`]. +#' @param language_tokens A tibble of language tokens returned by +#' [`get_language_tokens()`] to translate the string tokens in the data. +#' +#' @return A `tibble` containing the cleaned case data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) +#' locations_clean <- clean_locations(locations = locations) +#' +#' # other cleaned data required for `clean_cases()` +#' cases_vacc_history_clean <- clean_case_vax_history( +#' cases = cases, +#' language_tokens = language_tokens +#' ) +#' cases_address_history_clean <- clean_case_address_history( +#' cases = cases, +#' locations_clean = locations_clean, +#' language_tokens = language_tokens +#' ) +#' cases_dateranges_history_clean <- clean_case_med_history( +#' cases = cases, +#' language_tokens = language_tokens +#' ) +#' +#' cases_clean <- clean_cases( +#' cases = cases, +#' cases_address_history_clean = cases_address_history_clean, +#' cases_vacc_history_clean = cases_vacc_history_clean, +#' cases_dateranges_history_clean = cases_dateranges_history_clean, +#' language_tokens = language_tokens +#' ) +#' } +clean_cases <- function(cases, + cases_address_history_clean, + cases_vacc_history_clean, + cases_dateranges_history_clean, + language_tokens) { + + # Remove all deleted records + cases_clean <- dplyr::filter( + .data = cases, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # Remove all nested fields, otherwise problems with exporting to excel + cases_clean <- dplyr::select_if( + .tbl = cases_clean, + purrr::negate(is.list) + ) + + # take out all that are not core variables, otherwise diff versions and + # problems exporting to excel + cases_clean <- dplyr::select( + .data = cases_clean, + -dplyr::contains("questionnaireAnswers") + ) + + # standardize column name syntax + cases_clean <- janitor::clean_names(cases_clean) + + # label timestamps as datetime + cases_clean <- dplyr::rename( + .data = cases_clean, + date_of_birth = "dob", + datetime_updated_at = "updated_at", + datetime_created_at = "created_at" + ) + + # take out other unnecessary vars that are unnecessary and may confuse + # (i.e. was_case for cases) + cases_clean <- dplyr::select( + .data = cases_clean, + -c("is_date_of_onset_approximate", + "is_date_of_reporting_approximate", + "was_case", + "deleted", + "created_on") + ) + + #clean up all blank fields + cases_clean <- dplyr::mutate( + .data = cases_clean, + dplyr::across(dplyr::where(is.character), dplyr::na_if, "") + ) + + # clean date formats (TODO: edit this so that we can see time stamps) + cases_clean <- dplyr::mutate_at( + .tbl = cases_clean, + dplyr::vars(dplyr::starts_with("date_")), list(~ as.Date(substr(., 1, 10))) + ) + cases_clean <- dplyr::mutate( + .data = cases_clean, + datetime_updated_at = as.POSIXct(datetime_updated_at, format = "%Y-%m-%dT%H:%M") + ) + cases_clean <- dplyr::mutate( + .data = cases_clean, + datetime_created_at = as.POSIXct(datetime_created_at,format="%Y-%m-%dT%H:%M") + ) + + # translate responses of categorical vars so easier to read + cases_clean <- translate_categories( + data = cases_clean, + language_tokens = language_tokens + ) + + cases_clean <- dplyr::rename( + .data = cases_clean, + outcome = "outcome_id" + ) + + cases_clean <- dplyr::mutate( + .data = cases_clean, + isolated = dplyr::case_when(id %in% cases_dateranges_history_clean$id[cases_dateranges_history_clean$dateranges_typeid == "Isolation"] ~ TRUE, TRUE ~ FALSE) + ) + + cases_clean <- dplyr::mutate( + .data = cases_clean, + hospitalized = dplyr::case_when(id %in% cases_dateranges_history_clean$id[cases_dateranges_history_clean$dateranges_typeid == "Hospitalization"] ~ TRUE, TRUE ~ FALSE) + ) + + cases_clean <- dplyr::mutate( + .data = cases_clean, + icu = dplyr::case_when(id %in% cases_dateranges_history_clean$id[cases_dateranges_history_clean$dateranges_typeid == "ICU Admission"] ~ TRUE, TRUE ~ FALSE) + ) + + cases_address_history_clean <- dplyr::filter( + .data = cases_address_history_clean, + addresses_typeid == "Current address" + ) + + # join in current address from address history, only current place of residence + cases_clean <- dplyr::left_join(x = cases_clean, y = cases_address_history_clean, by = "id") + + # join in info from vacc block + cases_clean <- dplyr::mutate( + .data = cases_clean, + vaccinated = dplyr::case_when(id %in% cases_vacc_history_clean$id[cases_vacc_history_clean$vaccinesreceived_status == "VACCINATED"] ~ TRUE, TRUE ~ FALSE) + ) + + # force NA ages to appear as NA, not as 0 like sometimes occurs + cases_clean <- dplyr::mutate( + .data = cases_clean, + age_years = as.numeric(age_years) + ) + cases_clean <- dplyr::mutate( + .data = cases_clean, + age_years = dplyr::na_if(age_years, 0) + ) + cases_clean <- dplyr::mutate( + .data = cases_clean, + age_months = as.numeric(age_months) + ) + cases_clean <- dplyr::mutate( + .data = cases_clean, + age_months = dplyr::na_if(age_months, 0) + ) + + # standardize age vars into just one var, round by 1 decimal + cases_clean <- dplyr::mutate( + .data = cases_clean, + age = dplyr::case_when(!is.na(age_months) ~ round(age_months / 12, digits = 1), TRUE ~ age_years) + ) + + # WHO age categories updated Sept 2020: + # 0-4, 5-9, 10-14, 15-19, 20-29, 30-39, 40-49, 50-59, 60-64, 65-69, 70-74, + # 75-79, 80+ + # these categories below match that of detailed WHO surveillance dash: + # <5, 5-14, 15-24, 25-64, 65+ + cases_clean <- dplyr::mutate( + .data = cases_clean, + age_class = factor( + dplyr::case_when( + age <= 4 ~ "<5", + age <= 14 ~ "5-14", + age <= 24 ~ "15-24", + age <= 64 ~ "25-64", + is.finite(age) ~ "65+", + TRUE ~ "unknown" + ), + levels = c( + "<5", + "5-14", + "15-24", + "25-64", + "65+", + "unknown" + ) + ), + age_class = factor( + age_class, + levels = rev(levels(age_class)) + ) + ) + + # organize order of vars, only bring in what we need, take away confusing vars + cases_clean <- dplyr::select( + .data = cases_clean, + id, # identifier + visual_id, # identifier + classification, # identifier + first_name, # demographics + middle_name, # demographics + last_name, # demographics + gender, # demographics + age, # demographics + age_class, # demographics + occupation, # demographics + pregnancy_status, # demographics + date_of_reporting, # dates + date_of_onset, # dates + date_of_infection, # dates + date_become_case, # dates + date_of_burial, # dates + was_contact, # epi + risk_level, # epi + risk_reason, # epi + safe_burial, # epi + transfer_refused, # epi + responsible_user_id, # assigned contact tracer + matches("^admin_.*name$"), # address + lat, # address + long, # address + address, # address + postal_code, # address + city, # address + telephone, # address + email, # address + vaccinated, # vaccination & dateRanges block + isolated, # vaccination & dateRanges block + hospitalized, # vaccination & dateRanges block + icu, # vaccination & dateRanges block + outcome, # outcome + date_of_outcome, # outcome + location_id = addresses_locationid, # uuid in case need later for joining of whatever sort + created_by, # record modification + datetime_created_at, # record modification + updated_by, # record modification + datetime_updated_at # record modification + ) + + return(cases_clean) +} diff --git a/R/clean_contact_address_history.R b/R/clean_contact_address_history.R new file mode 100644 index 0000000..754cae6 --- /dev/null +++ b/R/clean_contact_address_history.R @@ -0,0 +1,118 @@ +#' Extracts address information from contact data +#' +#' @description This function un-nests and cleans the address data and stores +#' it in a standalone table with all addresses, even if there is more than 1 +#' per person. +#' +#' @param contacts A tibble with contacts data. Contacts data is returned by +#' [`get_contacts()`]. +#' @param locations_clean A tibble with cleaned locations data. Locations data +#' is returned by [`get_locations()`] and cleaned by [`clean_locations()`]. +#' @param language_tokens A tibble of language tokens returned by +#' [`get_language_tokens()`] to translate the string tokens in the data. +#' +#' @return A tibble with address information from contacts data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' contacts <- get_contacts( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) +#' locations_clean <- clean_locations(locations = locations) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' contact_address_history <- clean_contact_address_history( +#' contacts = contacts, +#' locations_clean = locations_clean, +#' language_tokens = language_tokens +#' ) +#' } +clean_contact_address_history <- function(contacts, + locations_clean, + language_tokens) { + + contacts_address_history_clean <- dplyr::filter( + .data = contacts, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + contacts_address_history_clean <- dplyr::select( + .data = contacts_address_history_clean, + "id", "visualId", "addresses" + ) + + contacts_address_history_clean <- tidyr::unnest( + data = contacts_address_history_clean, + cols = "addresses", + names_sep = "_" + ) + + contacts_address_history_clean <- dplyr::select_all( + .tbl = contacts_address_history_clean, + .funs = ~gsub("\\.", "_", tolower(.)) + ) + + contacts_address_history_clean <- dplyr::select_if( + .tbl = contacts_address_history_clean, + purrr::negate(is.list) + ) + + contacts_address_history_clean <- translate_categories( + data = contacts_address_history_clean, + language_tokens = language_tokens + ) + + contacts_address_history_clean <- dplyr::left_join( + x = contacts_address_history_clean, + y = locations_clean, + by = c("addresses_locationid" = "location_id") + ) + + # bring in GPS from locations if blank in contact record, otherwise use + # contact address block + contacts_address_history_clean <- dplyr::mutate( + .data = contacts_address_history_clean, + lat = dplyr::case_when( + is.na(addresses_geolocation_lat) ~ lat, TRUE ~ addresses_geolocation_lat), + long = dplyr::case_when( + is.na(addresses_geolocation_lng) ~ lat, TRUE ~ addresses_geolocation_lng) + ) + + contacts_address_history_clean <- dplyr::select( + .data = contacts_address_history_clean, + "id", + "addresses_locationid", + "addresses_typeid", + "lat", + "long", + "address" = addresses_addressline1, + "postal_code" = addresses_postalcode, + "city" = addresses_city, + "telephone" = addresses_phonenumber, + "email" = addresses_emailaddress, + dplyr::matches("^admin_.*name$") + ) + + return(contacts_address_history_clean) +} diff --git a/R/clean_contact_vax_history.R b/R/clean_contact_vax_history.R new file mode 100644 index 0000000..3125dfc --- /dev/null +++ b/R/clean_contact_vax_history.R @@ -0,0 +1,85 @@ +#' Cleans vaccination data from contact data +#' +#' @description Cleans and un-nests vaccination history, where vaccination is +#' complete, from contact data. Contact data is returned from +#' [`get_contacts()`]. +#' +#' @param contacts A tibble with address information from contact data. +#' @param language_tokens A tibble of language tokens returned by +#' [`get_language_tokens()`] to translate the string tokens in the data. +#' +#' @return A tibble with cleaned and un-nested vaccination history data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' contacts <- get_contacts( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' vax_history <- clean_contact_vax_history( +#' contacts = contacts, +#' language_tokens = language_tokens +#' ) +#' } +clean_contact_vax_history <- function(contacts, + language_tokens) { + + contacts_vax_history_clean <- dplyr::filter( + .data = contacts, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # cannot unnest on mix of data frames and lists so change empty lists to empty + # data frames + contacts_vax_history_clean$vaccinesReceived <- purrr::map( + contacts_vax_history_clean$vaccinesReceived, + .f = function(x) { + if (length(x) == 0) x <- data.frame() + x + } + ) + + contacts_vax_history_clean <- tidyr::unnest( + data = contacts_vax_history_clean, + cols = "vaccinesReceived", + names_sep = "_" + ) + + contacts_vax_history_clean <- dplyr::select_at( + .tbl = contacts_vax_history_clean, + dplyr::vars( + "id", + "visualId", + dplyr::starts_with("vaccinesReceived") + ), + .funs = tolower + ) + + contacts_vax_history_clean <- translate_categories( + data = contacts_vax_history_clean, + language_tokens = language_tokens + ) + + contacts_vax_history_clean <- dplyr::mutate_at( + .tbl = contacts_vax_history_clean, + dplyr::vars(vaccinesreceived_date), as.Date + ) + + return(contacts_vax_history_clean) +} diff --git a/R/clean_contacts.R b/R/clean_contacts.R new file mode 100644 index 0000000..5e8bd37 --- /dev/null +++ b/R/clean_contacts.R @@ -0,0 +1,299 @@ +#' Clean contacts data +#' +#' @description Cleans and un-nests contact data. Contact data is returned by +#' [`get_contacts()`]. +#' +#' @param contacts A `tibble` containing the contact data. +#' @param contacts_address_history_clean A `tibble` containing the cleaned +#' address history data from contacts (data is cleaned by +#' [`clean_contact_address_history()`]. +#' @param contacts_vacc_history_clean A `tibble` containing the cleaned +#' vaccination history data from contacts (data is cleaned by +#' [`clean_contact_vax_history()`]. +#' @param contacts_becoming_cases A `tibble` containing the cleaned data on +#' contacts that became cases (date is produced using +#' [`cases_from_contacts()`]). +#' +#' @return A `tibble` containing the cleaned case data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' contacts <- get_contacts( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) +#' +#' locations_clean <- clean_locations(locations = locations) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' # other cleaned data required for `clean_contacts()` +#' contacts_vacc_history_clean <- clean_contact_vax_history( +#' contacts = contacts, +#' language_tokens = language_tokens +#' ) +#' contacts_address_history_clean <- clean_contact_address_history( +#' contacts = contacts, +#' locations_clean = locations_clean, +#' language_tokens = language_tokens +#' ) +#' +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' cases_address_history_clean <- clean_case_address_history( +#' cases = cases, +#' locations_clean = locations_clean, +#' language_tokens = language_tokens +#' ) +#' cases_vacc_history_clean <- clean_case_vax_history( +#' cases = cases, +#' language_tokens = language_tokens +#' ) +#' cases_dateranges_history_clean <- clean_case_med_history( +#' cases = cases, +#' language_tokens = language_tokens +#' ) +#' +#' cases_clean <- clean_cases( +#' cases = cases, +#' cases_address_history_clean = cases_address_history_clean, +#' cases_vacc_history_clean = cases_vacc_history_clean, +#' cases_dateranges_history_clean = cases_dateranges_history_clean, +#' language_tokens = language_tokens +#' ) +#' contacts_becoming_cases <- cases_from_contacts(cases_clean = cases_clean) +#' +#' contacts_clean <- clean_contacts( +#' contacts = contacts, +#' contacts_address_history_clean = cases_address_history_clean, +#' contacts_vacc_history_clean = cases_vacc_history_clean, +#' contacts_becoming_cases = contacts_becoming_cases +#' ) +#' } +clean_contacts <- function(contacts, + contacts_address_history_clean, + contacts_vacc_history_clean, + contacts_becoming_cases) { + + # Remove all deleted records + contacts_clean <- dplyr::filter( + .data = contacts, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # Remove all nested fields, otherwise problems with exporting to excel + contacts_clean <- dplyr::select_if( + .tbl = contacts_clean, + .predicate = purrr::negate(is.list) + ) + + # take out all that are not core variables, otherwise diff versions and + # problems exporting to excel + contacts_clean <- dplyr::select( + .data = contacts_clean, + -dplyr::contains("questionnaireAnswers") + ) + + # standardize column name syntax + contacts_clean <- janitor::clean_names(dat = contacts_clean) + + # label timestamps as datetime + contacts_clean <- dplyr::rename( + .data = contacts_clean, + date_of_birth = "dob", + date_of_follow_up_start = "follow_up_start_date", + date_of_follow_up_end = "follow_up_end_date", + datetime_updated_at = "updated_at", + datetime_created_at = "created_at" + ) + + # take out other unnecessary vars that are unnecessary and may confuse + # (i.e. was_case for cases) + contacts_clean <- dplyr::select( + .data = contacts_clean, + -c( + "is_date_of_reporting_approximate", + "was_contact", + "follow_up_original_start_date", + "type", + "deleted", + "created_on" + ) + ) + + #clean up all character fields + contacts_clean <- dplyr::mutate( + .data = contacts_clean, + dplyr::across(dplyr::where(is.character), na_if, "") + ) + + # clean date formats (TODO: edit this so that we can see time stamps) + contacts_clean <- dplyr::mutate_at( + .tbl = contacts_clean, + .vars = dplyr::vars(dplyr::starts_with("date_")), + list(~ as.Date(substr(., 1, 10))) + ) + + contacts_clean <- dplyr::mutate( + .data = contacts_clean, + datetime_updated_at = as.POSIXct(datetime_updated_at, format = "%Y-%m-%dT%H:%M") + ) + + contacts_clean <- dplyr::mutate( + .data = contacts_clean, + datetime_created_at = as.POSIXct(datetime_created_at, format = "%Y-%m-%dT%H:%M") + ) + + # translate responses of categorical vars so easier to read + contacts_clean <- translate_categories( + data = contacts_clean, + language_tokens = language_tokens + ) + + contacts_clean <- dplyr::rename( + .data = contacts_clean, + outcome = "outcome_id", + relationship_certainty_level = "relationship_certainty_level_id", + relationship_exposure_type = "relationship_exposure_type_id", + relationship_context_of_transmission = "relationship_social_relationship_type_id", + relationship_exposure_frequency = "relationship_exposure_frequency_id", + relationship_exposure_duration = "relationship_exposure_duration_id" + ) + + contacts_address_history_clean <- dplyr::filter( + .data = contacts_address_history_clean, + addresses_typeid == "Current address" + ) + + # join in current address from address history, only current place of residence + contacts_clean <- dplyr::left_join( + x = contacts_clean, + y = contacts_address_history_clean, + by = "id" + ) + + # join in info from vacc block + contacts_clean <- dplyr::mutate( + .data = contacts_clean, + vaccinated = case_when(id %in% contacts_vacc_history_clean$id[contacts_vacc_history_clean$vaccinesreceived_status == "Vaccinated"] ~ TRUE, TRUE ~ FALSE) + ) + + # force NA ages to appear as NA, not as 0 like sometimes occurs + contacts_clean <- dplyr::mutate(.data = contacts_clean, age_years = as.numeric(age_years)) + contacts_clean <- dplyr::mutate(.data = contacts_clean, age_years = na_if(age_years,0)) + contacts_clean <- dplyr::mutate(.data = contacts_clean, age_months = as.numeric(age_months)) + contacts_clean <- dplyr::mutate(.data = contacts_clean, age_months = na_if(age_months,0)) + + # standardize age vars into just one var, round by 1 decimal + contacts_clean <- dplyr::mutate( + .data = contacts_clean, + age = case_when(!is.na(age_months) ~ round(age_months / 12, digits = 1), + TRUE ~ age_years)) + + # WHO age categories updated Sept 2020: + # 0-4, 5-9, 10-14, 15-19, 20-29, 30-39, 40-49, 50-59, 60-64, 65-69, 70-74, + # 75-79, 80+ + # these categories below match that of detailed WHO surveillance dash: + # <5, 5-14, 15-24, 25-64, 65+ + contacts_clean <- dplyr::mutate( + .data = contacts_clean, + age_class = factor( + case_when( + age <= 4 ~ "<5", + age <= 14 ~ "5-14", + age <= 24 ~ "15-24", + age <= 64 ~ "25-64", + is.finite(age) ~ "65+", + TRUE ~ "unknown" + ), levels = c( + "<5", + "5-14", + "15-24", + "25-64", + "65+", + "unknown" + )), + age_class = factor( + age_class, + levels = rev(levels(age_class))) + ) + + # organize order of vars, only bring in what we need, take away confusing vars + contacts_clean <- dplyr::select( + .data = contacts_clean, + id, # identifier + visual_id, # identifier + classification, # identifier + follow_up_status, # identifier + first_name, # demographics + middle_name, # demographics + last_name, # demographics + gender, # demographics + age, # demographics + age_class, # demographics + occupation, # demographics + pregnancy_status, # demographics + date_of_reporting, # dates + date_of_last_contact, # dates + date_of_burial, # dates + date_of_follow_up_start, # dates + date_of_follow_up_end, # dates + was_case, # epi + risk_level, # epi + risk_reason, # epi + safe_burial, # epi + transfer_refused, # epi + responsible_user_id, # assigned contact tracer + follow_up_team_id, # assigned contact tracer + matches("^admin_.*name$"), + lat, # address + long, # address + address, # address + postal_code, # address + city, # address + telephone, # address + email, # address + vaccinated, # vaccination + outcome, # outcome + date_of_outcome, # outcome + relationship_exposure_type, + relationship_context_of_transmission, + relationship_exposure_duration, + relationship_exposure_frequency, + relationship_certainty_level, + relationship_cluster_id, + location_id = addresses_locationid, # uuid in case need later for joining of whatever sort. + created_by, # record modification + datetime_created_at, # record modification + updated_by, # record modification + datetime_updated_at # record modification + ) + + #Join in cases that used to be contacts + contacts_clean <- dplyr::bind_rows(contacts_clean, contacts_becoming_cases) + + return(contacts_clean) +} diff --git a/R/clean_contacts_of_contacts.R b/R/clean_contacts_of_contacts.R new file mode 100644 index 0000000..1c25de8 --- /dev/null +++ b/R/clean_contacts_of_contacts.R @@ -0,0 +1,232 @@ +#' Clean contacts of contacts data +#' +#' @description Cleans and un-nests contacts of contacts data. Contacts of +#' contacts data is returned by [`get_contacts_of_contacts()`]. +#' +#' @param contacts_of_contacts A `tibble` containing the contacts of contacts +#' data. +#' @param contacts_of_contacts_address_history_clean A `tibble` containing the +#' cleaned address history data from contacts of contacts (data is cleaned by +#' [`clean_contacts_of_contacts_address_history()`]). +#' @param contacts_of_contacts_vacc_history_clean A `tibble` containing the +#' cleaned vaccination history from contacts of contacts (data is cleaned by +#' [`clean_contacts_of_contacts_vax_history()`]). +#' @param language_tokens A tibble of language tokens returned by +#' [`get_language_tokens()`] to translate the string tokens in the data. +#' +#' @return A `tibble` containing the cleaned contacts of contacts data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' contacts_of_contacts <- get_contacts_of_contacts( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) +#' +#' locations_clean <- clean_locations(locations = locations) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' contacts_of_contacts_address_history_clean <- clean_contacts_of_contacts_address_history( +#' contacts_of_contacts = contacts_of_contacts, +#' locations_clean = locations_clean, +#' language_tokens = language_tokens +#' ) +#' +#' contacts_of_contacts_vacc_history_clean <- clean_contacts_of_contacts_vax_history( +#' contacts_of_contacts = contacts_of_contacts, +#' language_tokens = language_tokens +#' ) +#' +#' contacts_of_contacts_clean <- clean_contacts_of_contacts( +#' contacts_of_contacts = contacts_of_contacts, +#' contacts_of_contacts_address_history_clean = contacts_of_contacts_address_history_clean, +#' contacts_of_contacts_vacc_history_clean = contacts_of_contacts_vacc_history_clean, +#' language_tokens = language_tokens +#' ) +#' } +clean_contacts_of_contacts <- function(contacts_of_contacts, + contacts_of_contacts_address_history_clean, + contacts_of_contacts_vacc_history_clean, + language_tokens) { + + # Remove all deleted records + coc_clean <- dplyr::filter( + .data = contacts_of_contacts, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # Remove all nested fields, otherwise problems with exporting to excel + coc_clean <- dplyr::select_if( + .tbl = coc_clean, + .predicate = purrr::negate(is.list) + ) + + # standardize column name syntax + coc_clean <- janitor::clean_names(dat = coc_clean) + + # label timestamps as datetime + coc_clean <- dplyr::rename( + .data = coc_clean, + date_of_birth = "dob", + datetime_updated_at = "updated_at", + datetime_created_at = "created_at" + ) + + # clean up all character fields + coc_clean <- dplyr::mutate( + .data = coc_clean, + dplyr::across(dplyr::where(is.character), na_if, "") + ) + + # clean date formats (TODO: edit this so that we can see time stamps) + coc_clean <- dplyr::mutate_at( + .tbl = coc_clean, + .vars = dplyr::vars(dplyr::starts_with("date_")), + list(~ as.Date(substr(., 1, 10))) + ) + + coc_clean <- dplyr::mutate( + .data = coc_clean, + datetime_updated_at = as.POSIXct(datetime_updated_at,format="%Y-%m-%dT%H:%M") + ) + + coc_clean <- dplyr::mutate( + .data = coc_clean, + datetime_created_at = as.POSIXct(datetime_created_at,format="%Y-%m-%dT%H:%M") + ) + + # truncate responses of categorical vars so easier to read + coc_clean <- translate_categories( + data = coc_clean, + language_tokens = language_tokens + ) + + contacts_of_contacts_address_history_clean <- dplyr::filter( + .data = contacts_of_contacts_address_history_clean, + addresses_typeid == "Current address" + ) + + # join in current address from address history, only current place of + # residence + coc_clean <- left_join( + x = coc_clean, + y = contacts_of_contacts_address_history_clean, + by="id" + ) + + # join in info from vacc block + coc_clean <- dplyr::mutate( + .data = coc_clean, + vaccinated = case_when(id %in% contacts_of_contacts_vacc_history_clean$id[contacts_of_contacts_vacc_history_clean$vaccinesreceived_status == "VACCINATED"] ~ TRUE, TRUE ~ FALSE) + ) + + # force NA ages to appear as NA, not as 0 like sometimes occurs + coc_clean <- dplyr::mutate(.data = coc_clean, age_years = as.numeric(age_years)) + coc_clean <- dplyr::mutate(.data = coc_clean, age_years = na_if(age_years,0)) + coc_clean <- dplyr::mutate(.data = coc_clean, age_months = as.numeric(age_months)) + coc_clean <- dplyr::mutate(.data = coc_clean, age_months = na_if(age_months,0)) + + # standardize age vars into just one var, round by 1 decimal + coc_clean <- dplyr::mutate( + .data = coc_clean, + age = case_when(!is.na(age_months) ~ round(age_months / 12, digits = 1), + TRUE ~ age_years) + ) + + # WHO age categories updated Sept 2020: + # 0-4, 5-9, 10-14, 15-19, 20-29, 30-39, 40-49, 50-59, 60-64, 65-69, 70-74, + # 75-79, 80+ + # these categories below match that of detailed WHO surveillance dash: + # <5, 5-14, 15-24, 25-64, 65+ + coc_clean <- dplyr::mutate( + .data = coc_clean, + age_class = factor( + case_when( + age <= 4 ~ "<5", + age <= 14 ~ "5-14", + age <= 24 ~ "15-24", + age <= 64 ~ "25-64", + is.finite(age) ~ "65+", + TRUE ~ "unknown" + ), levels = c( + "<5", + "5-14", + "15-24", + "25-64", + "65+", + "unknown" + )), + age_class = factor( + age_class, + levels = rev(levels(age_class))) + ) + + # organize order of vars, only bring in what we need, take away confusing vars + coc_clean <- dplyr::select( + .data = coc_clean, + "id", # identifier + "visual_id", # identifier + "classification", # identifier + "first_name", # demographics + "middle_name", # demographics + "last_name", # demographics + "gender", # demographics + "age", # demographics + "age_class", # demographics + "occupation", # demographics + "pregnancy_status", # demographics + "date_of_reporting", # dates + "date_of_last_contact", # dates + "date_of_burial", # dates + "was_case", # epi + "risk_level", # epi + "risk_reason", # epi + "safe_burial", # epi + "transfer_refused", # epi + "responsible_user_id", # assigned contact tracer + dplyr::matches("^admin_.*name$"), # address + "lat",# address + "long", # address + "address", # address + "postal_code", # address + "city", # address + "telephone", # address + "email", # address + "vaccinated", # vaccination + "outcome", # outcome + "date_of_outcome", # outcome + "relationship_exposure_type", + "relationship_context_of_transmission", + "relationship_exposure_duration", + "relationship_exposure_frequency", + "relationship_certainty_level", + "relationship_cluster_id", + location_id = "addresses_locationid", # uuid in case need later for joining of whatever sort. + "created_by", + "datetime_created_at", + "updated_by", + "datetime_updated_at" + ) + + return(coc_clean) +} diff --git a/R/clean_contacts_of_contacts_address_history.R b/R/clean_contacts_of_contacts_address_history.R new file mode 100644 index 0000000..8bca0d2 --- /dev/null +++ b/R/clean_contacts_of_contacts_address_history.R @@ -0,0 +1,118 @@ +#' Extracts address information from contacts of contacts data +#' +#' @description This function un-nests and cleans the address data and stores +#' it in a standalone table with all addresses, even if there is more than 1 +#' per person. +#' +#' @param contacts_of_contacts A`tibble` with contacts of contacts data. +#' Contacts of contacts data is returned by [`get_contacts_of_contacts()`]. +#' @param locations_clean A `tibble` with cleaned location data. Location data +#' is returned by [`get_locations()`] and cleaned by [`clean_locations()`]. +#' @param language_tokens A tibble of language tokens returned by +#' [`get_language_tokens()`] to translate the string tokens in the data. +#' +#' @return A `tibble` with address information from contacts of contacts data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' contacts_of_contacts <- get_contacts_of_contacts( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) +#' locations_clean <- clean_locations(locations = locations) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' contact_of_contacts_add_hist <- clean_contacts_of_contacts_address_history( +#' contacts_of_contacts = contacts_of_contacts, +#' locations_clean = locations_clean, +#' language_tokens = language_tokens +#' ) +#' } +clean_contacts_of_contacts_address_history <- function(contacts_of_contacts, + locations_clean, + language_tokens) { + + coc_add_hist <- dplyr::filter( + .data = contacts_of_contacts, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + coc_add_hist <- dplyr::select( + .data = coc_add_hist, + "id", "visualId", "addresses" + ) + + coc_add_hist <- tidyr::unnest( + data = coc_add_hist, + "addresses", + names_sep = "_" + ) + + coc_add_hist <- dplyr::select_all( + .tbl = coc_add_hist, + .funs = ~gsub("\\.", "_", tolower(.)) + ) + + coc_add_hist <- dplyr::select_if( + .tbl = coc_add_hist, + .predicate = purrr::negate(is.list) + ) + + coc_add_hist <- translate_categories( + data = coc_add_hist, + language_tokens = language_tokens + ) + + coc_add_hist <- dplyr::left_join( + x = coc_add_hist, + y = locations_clean, + by = c("addresses_locationid" = "location_id") + ) + + # bring in GPS from locations if blank in contact record, otherwise use + # contact address block + coc_add_hist <- dplyr::mutate( + .data = coc_add_hist, + lat = case_when( + is.na(addresses_geolocation_lat) ~ lat, TRUE ~ addresses_geolocation_lat), + long = case_when( + is.na(addresses_geolocation_lng) ~ lat, TRUE ~ addresses_geolocation_lng) + ) + + coc_add_hist <- dplyr::select( + .data = coc_add_hist, + "id", + "addresses_locationid", + "addresses_typeid", + "lat", + "long", + address = "addresses_addressline1", + postal_code = "addresses_postalcode", + city = "addresses_city", + telephone = "addresses_phonenumber", + email = "addresses_emailaddress", + dplyr::matches("^admin_.*name$") + ) + + return(coc_add_hist) +} diff --git a/R/clean_contacts_of_contacts_vax_history.R b/R/clean_contacts_of_contacts_vax_history.R new file mode 100644 index 0000000..10c8244 --- /dev/null +++ b/R/clean_contacts_of_contacts_vax_history.R @@ -0,0 +1,73 @@ +#' Cleans vaccination data from contacts of contacts data +#' +#' @description Cleans and un-nests vaccination history, where vaccination is +#' complete, from contacts of contacts data. Contacts of contacts data is +#' returned from [`get_contacts_of_contacts()`]. +#' +#' @param contacts_of_contacts A `tibble` with address information from contacts +#' of contacts data. +#' @param language_tokens A tibble of language tokens returned by +#' [`get_language_tokens()`] to translate the string tokens in the data. +#' +#' @return A `tibble` with cleaned and un-nested vaccination history data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' contacts_of_contacts <- get_contacts_of_contacts( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' vax_history <- clean_contacts_of_contacts_vax_history( +#' contacts_of_contacts = contacts_of_contacts, +#' language_tokens = language_tokens +#' ) +#' } +clean_contacts_of_contacts_vax_history <- function(contacts_of_contacts, + language_tokens) { + + coc_vacc_hist <- dplyr::filter( + .data = contacts_of_contacts, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + coc_vacc_hist <- tidyr::unnest( + data = coc_vacc_hist, + cols = "vaccinesReceived", + names_sep = "_" + ) + + coc_vacc_hist <- dplyr::select_at( + .tbl = coc_vacc_hist, + .vars = dplyr::vars(id, visualId, dplyr::starts_with("vaccinesReceived")), + tolower + ) + + coc_vacc_hist <- translate_categories( + data = coc_vacc_hist, + language_tokens = language_tokens + ) + + coc_vacc_hist <- dplyr::mutate_at( + .tbl = coc_vacc_hist, + dplyr::vars(vaccinesreceived_date), + as.Date + ) + + return(coc_vacc_hist) +} diff --git a/R/clean_events.R b/R/clean_events.R new file mode 100644 index 0000000..1288477 --- /dev/null +++ b/R/clean_events.R @@ -0,0 +1,116 @@ +#' Clean events data +#' +#' @description Cleans and un-nests events data which is returned from +#' [`get_events()`]. +#' +#' @param events A `tibble` with events data. Events data is returned by +#' [`get_events()`]. +#' @param locations_clean A `tibble` with cleaned location data. Location data +#' is returned by [`get_locations()`] and cleaned by [`clean_locations()`]. +#' Make sure the locations data is cleaned prior to supplying it to +#' `clean_events()`. +#' +#' @return A `tibble` with cleaned events data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' events <- get_events( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) +#' locations_clean <- clean_locations(locations = locations) +#' +#' clean_events <- clean_events( +#' events = events, +#' locations_clean = locations_clean) +#' } +clean_events <- function(events, + locations_clean) { + + # Remove all deleted records + clean_events <- dplyr::filter( + .data = events, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # Remove all nested fields, otherwise problems with exporting to excel + clean_events <- dplyr::select_if(.tbl = clean_events, purrr::negate(is.list)) + + # standardize column name syntax + clean_events <- janitor::clean_names(clean_events) + + # label timestamps as datetime + clean_events <- dplyr::rename( + .data = clean_events, + datetime_updated_at = "updated_at", + datetime_created_at = "created_at" + ) + + # clean up all character fields + clean_events <- dplyr::mutate( + .data = clean_events, + dplyr::across(dplyr::where(is.character), na_if, "") + ) + + # clean date formats (TODO: edit this so that we can see time stamps) + clean_events <- mutate_at( + clean_events, + dplyr::vars(dplyr::starts_with("date_")), + list(~ as.Date(substr(., 1, 10))) + ) + clean_events <- mutate( + clean_events, + datetime_updated_at = as.POSIXct(datetime_updated_at, format="%Y-%m-%dT%H:%M")) + clean_events <- mutate( + clean_events, + datetime_created_at = as.POSIXct(datetime_created_at,format="%Y-%m-%dT%H:%M")) + + clean_events <- dplyr::left_join( + x = clean_events, + y = select(locations_clean, + location_id, + matches("^admin_.*name$")), + by = c("address_location_id" = "location_id") + ) + + # organize order of vars, only bring in what we need, take away + # confusing vars + clean_events <- dplyr::select( + .data = clean_events, + "id", # identifier + "name", # identifier + "date", # dates + "date_of_reporting", # dates + "description", + "responsible_user", # assigned contact tracer + matches("^admin_.*name$"), + lat = "address_geo_location_lat", # address + long = "address_geo_location_lng", # address + address = "address_address_line1", # address + postal_code = "address_postal_code", # address + city = "address_city", # address + telephone = "address_phone_number", # address + email = "address_email_address", # address + location_id = "address_location_id", # uuid in case need later for joining of whatever sort. + "created_by", + "datetime_created_at", + "updated_by", + "datetime_updated_at" + ) # record modification + + return(clean_events) +} diff --git a/R/clean_followups.R b/R/clean_followups.R new file mode 100644 index 0000000..1be33d5 --- /dev/null +++ b/R/clean_followups.R @@ -0,0 +1,168 @@ +#' Clean followup data +#' +#' @description Cleans and un-nests followup data which is returned from +#' [`get_followups()`] +#' +#' @param followups A `tibble` with events data. Followup data is returned by +#' [`get_followups()`]. +#' @param contacts_address_history_clean A `tibble` with cleaned address +#' history data from contacts. Contacts data is returned by [`get_contacts()`] +#' and cleaned by [`clean_contact_address_history()`]. +#' +#' @return A `tibble` with cleaned followup data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' followups <- get_followups( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' contacts <- get_contacts( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) +#' +#' locations_clean <- clean_locations(locations = locations) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' contacts_address_history_clean <- clean_contact_address_history( +#' contacts = contacts, +#' locations_clean = locations_clean, +#' language_tokens = language_tokens +#' ) +#' +#' followups_clean <- clean_followups( +#' followups = followups, +#' contacts_address_history_clean = contacts_address_history_clean, +#' language_tokens = language_tokens +#' ) +#' } +clean_followups <- function(followups, + contacts_address_history_clean, + language_tokens) { + + # Remove all deleted records + followups_clean <- dplyr::filter( + .data = followups, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # Remove all nested fields, otherwise problems with exporting to excel + followups_clean <- dplyr::select_if( + .tbl = followups_clean, + .predicate = purrr::negate(is.list) + ) + + # take out all that are not core variables, otherwise diff versions and + # problems exporting to excel + followups_clean <- dplyr::select( + .data = followups_clean, + -dplyr::contains("questionnaireAnswers") + ) + + # standardize column name syntax + followups_clean <- janitor::clean_names(dat = followups_clean) + + # label timestamps as datetime + followups_clean <- dplyr::rename( + .data = followups_clean, + datetime_updated_at = "updated_at", + datetime_created_at = "created_at" + ) + + # clean up all character fields + followups_clean <- dplyr::mutate( + .data = followups_clean, + dplyr::across(dplyr::where(is.character), dplyr::na_if, "") + ) + + # clean date formats (TODO: edit this so that we can see time stamps) + followups_clean <- dplyr::mutate_at( + .tbl = followups_clean, + dplyr::vars(date), list(~ as.Date(substr(., 1, 10))) + ) + + followups_clean <- dplyr::mutate( + .data = followups_clean, + datetime_updated_at = as.POSIXct(datetime_updated_at, format = "%Y-%m-%dT%H:%M") + ) + + followups_clean <- dplyr::mutate( + .data = followups_clean, + datetime_created_at = as.POSIXct(datetime_created_at, format = "%Y-%m-%dT%H:%M") + ) + + # translate responses of categorical vars so easier to read + followups_clean <- translate_categories( + data = followups_clean, + language_tokens = language_tokens + ) + + followups_clean <- dplyr::rename( + .data = followups_clean, + followup_status = "status_id" + ) + + contacts_address_history_clean <- dplyr::filter( + .data = contacts_address_history_clean, + addresses_typeid == "Current address" + ) + + followups_clean <- dplyr::left_join( + x = followups_clean, + y = contacts_address_history_clean, + by = "id" + ) + + # organize order of vars, only bring in what we need, take away confusing vars + followups_clean <- dplyr::select( + .data = followups_clean, + "id", # identifier + "contact_id", # identifier + "contact_visual_id", # identifier + "date", # dates + followup_number = "index", # FU status + "followup_status", # FU status + "targeted", # FU status + "responsible_user_id", # assigned contact tracer + "team_id", # assigned contact tracer + dplyr::matches("^admin_.*name$"), # address + "lat", # address + "long", # address + "address", # address + "postal_code", # address + "city", # address + "telephone", # address + "email", # address + location_id = "addresses_locationid", # uuid in case need later for joining of whatever sort. + "created_by", # record modification + "datetime_created_at", # record modification + "updated_by", # record modification + "datetime_updated_at" # record modification + ) + + return(followups_clean) +} diff --git a/R/clean_locations.R b/R/clean_locations.R new file mode 100644 index 0000000..49bf1f3 --- /dev/null +++ b/R/clean_locations.R @@ -0,0 +1,138 @@ +#' Cleans location data +#' +#' @description Rearrange via joins to get into more usable hierarchy format, +#' these can then be joined to cases, contacts, etc for further analysis +#' +#' @param locations A [`tibble`] containing locations data. This is the data +#' returned from [`get_locations()`] +#' +#' @return A `tibble` containing the cleaned and rearranged location data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' clean_locations(locations = locations, language_tokens = language_tokens) +#' } +clean_locations <- function(locations, + language_tokens) { + + # filter out delete and inactive (or NA) values + clean_locations <- dplyr::filter( + locations, + .data$deleted == FALSE | is.na(.data$deleted) + ) + clean_locations <- dplyr::filter( + clean_locations, + .data$active == TRUE | is.na(.data$active) + ) + + clean_locations <- translate_categories( + data = clean_locations, + language_tokens = language_tokens + ) + + # add admin-level column + clean_locations <- dplyr::rename( + .data = clean_locations, + admin_level = "geographicalLevelId" + ) + + # select columns + clean_locations <- dplyr::select( + .data = clean_locations, + location_id = "id", + "admin_level", + "name", + parent_location_id = "parentLocationId", + lat = "geoLocation.lat", + long = "geoLocation.lng" + ) + + clean_locations <- dplyr::filter( + .data = clean_locations, + !is.na(.data$admin_level) + ) + + # split locations data frames into separate data frames by admin level + locations_split <- dplyr::group_by( + .data = clean_locations, + .data$admin_level + ) + locations_split <- dplyr::group_split(locations_split) + + # rename columns by appending admin level + locations_split <- purrr::imap(.x = locations_split, .f = function(x, idx) { + colnames(x) <- paste("admin", idx - 1, colnames(x), sep = "_") + x + }) + + # add location_id and parent_location_id columns + locations_split <- purrr::imap(.x = locations_split, .f = function(x, idx) { + x$location_id <- dplyr::pull( + x, paste("admin", idx - 1, "location_id", sep = "_") + ) + x + }) + locations_split <- purrr::imap(.x = locations_split, .f = function(x, idx) { + if (idx - 1 != 0) { + x$parent_location_id <- dplyr::pull( + x, paste("admin", idx - 1, "parent_location_id", sep = "_") + ) + } + x + }) + + # loop over list of admin specific data frames and join them + for (i in seq(from = length(locations_split), to = 2L)) { + + for (x in 1:(i - 1)) { + + join_index <- i - x + + locations_split[[i]] <- dplyr::left_join( + x = locations_split[[i]], + y = locations_split[[join_index]], + by = c("parent_location_id" = "location_id") + ) + + # first table (admin level 0) does not contain parent_location_id so skip + if (join_index != 1) { + # use parent_location_id from right table + locations_split[[i]]$parent_location_id <- + locations_split[[i]]$parent_location_id.y + # remove extra parent_location_id column + locations_split[[i]]$parent_location_id.y <- NULL + } + } + locations_split[[i]]$parent_location_id <- NULL + } + + # bind the admin level tables by row + full <- do.call(dplyr::bind_rows, locations_split) + + # join cleaned location with new table + clean_locations <- left_join( + x = clean_locations, + y = full, + by = "location_id" + ) + + return(clean_locations) +} diff --git a/R/clean_relationships.R b/R/clean_relationships.R new file mode 100644 index 0000000..afbe7d5 --- /dev/null +++ b/R/clean_relationships.R @@ -0,0 +1,114 @@ +#' Cleans relationship data +#' +#' @description Cleans and un-nests relationship data. Relationship data is +#' returned by [`get_relationships()`]. +#' +#' @param relationships A `tibble` of relationship data. Relationship data is +#' returned by [`get_relationships()`]. +#' +#' @return A `tibble` with clean relationship data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' relationships <- get_relationships( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' clean_relationships <- clean_relationships( +#' relationships, +#' language_tokens = language_tokens +#' ) +#' } +clean_relationships <- function(relationships, + language_tokens) { + + # Remove all deleted records + clean_relationships <- dplyr::filter( + .data = relationships, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # Remove all nested fields, otherwise problems with exporting to excel + clean_relationships <- dplyr::select_if( + .tbl = clean_relationships, + purrr::negate(is.list) + ) + + # standardize column name syntax + clean_relationships <- janitor::clean_names(clean_relationships) + + # label timestamps as datetime + clean_relationships <- dplyr::rename( + .data = clean_relationships, + datetime_updated_at = "updated_at", + datetime_created_at = "created_at" + ) + + #clean up all character fields + clean_relationships <- dplyr::mutate( + .data = clean_relationships, + dplyr::across(dplyr::where(is.character), na_if, "") + ) + + # clean date formats (TODO: edit this so that we can see time stamps) + clean_relationships <- dplyr::mutate( + .data = clean_relationships, + dplyr::across( + dplyr::starts_with("date_"), list(~ as.Date(substr(., 1, 10))) + ) + ) + clean_relationships <- dplyr::mutate( + .data = clean_relationships, + datetime_updated_at = as.POSIXct( + datetime_updated_at, + format = "%Y-%m-%dT%H:%M" + ) + ) + clean_relationships <- dplyr::mutate( + .data = clean_relationships, + datetime_created_at = as.POSIXct( + datetime_created_at, + format = "%Y-%m-%dT%H:%M" + ) + ) + + # translate responses of categorical vars so easier to read + clean_relationships <- translate_categories( + data = clean_relationships, + language_tokens = language_tokens + ) + + # organize order of vars, only bring in what we need, take away confusing vars + clean_relationships <- dplyr::select( + .data = clean_relationships, + "id", #id + "source_person_id", #id + "source_person_visual_id", #id + "target_person_id", #id + "target_person_visual_id", #id + "source_person_type", #id + "target_person_type", #id + "created_by", # record modification + "datetime_created_at", # record modification + "updated_by", # record modification + "datetime_updated_at" # record modification + ) + + return(clean_relationships) +} diff --git a/R/clean_teams.R b/R/clean_teams.R new file mode 100644 index 0000000..6c670d0 --- /dev/null +++ b/R/clean_teams.R @@ -0,0 +1,75 @@ +#' Clean teams data +#' +#' @description Cleans and un-nests teams data. Teams data is returned by +#' [`get_teams()`]. +#' +#' @param teams A `tibble` containing teams data. Teams data is returned by +#' [`get_teams()`]. +#' +#' @return A `tibble` of cleaned teams data +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' +#' teams <- get_teams( +#' url = url, +#' username = username, +#' password = password +#' ) +#' +#' clean_teams <- clean_teams(teams) +#' } +clean_teams <- function(teams) { + + # Remove all deleted records + clean_teams <- dplyr::filter( + .data = teams, + .data$deleted == FALSE | is.na(.data$deleted) + ) + + # standardize column name syntax + clean_teams <- janitor::clean_names(clean_teams) + + # label timestamps as datetime + clean_teams <- dplyr::rename( + .data = clean_teams, + datetime_updated_at = "updated_at", + datetime_created_at = "created_at" + ) + + #clean up all character fields + clean_teams <- dplyr::mutate( + .data = clean_teams, + dplyr::across(dplyr::where(is.character), na_if, "") + ) + + clean_teams <- tidyr::unnest_wider( + data = clean_teams, + col = "user_ids", + names_sep = "_" + ) + clean_teams <- tidyr::unnest_wider( + data = clean_teams, + col = "location_ids", + names_sep = "_" + ) + + # organize order of vars, only bring in what we need, take away confusing vars + clean_teams <- dplyr::select( + .data = clean_teams, + "id", + "name", + dplyr::starts_with("user_ids"), + dplyr::starts_with("location_ids"), + "created_by", # record modification + "datetime_created_at", # record modification + "updated_by", # record modification + "datetime_updated_at" # record modification + ) + + return(clean_teams) +} diff --git a/R/clean_users.R b/R/clean_users.R new file mode 100644 index 0000000..7bc15c9 --- /dev/null +++ b/R/clean_users.R @@ -0,0 +1,77 @@ +#' Cleans users data +#' +#' @description Cleans and un-nests users data. Users data is returned by +#' [`get_users()`]. +#' +#' @param users A `tibble` containing users data. Users data is returned by +#' [`get_users()`]. +#' +#' @return A `tibble` with cleaned users data. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' +#' users <- get_users( +#' url = url, +#' username = username, +#' password = password +#' ) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' clean_users <- clean_users(users = users, language_tokens = language_tokens) +#' } +clean_users <- function(users, + language_tokens) { + + # standardize column name syntax + clean_users <- janitor::clean_names(users) + + # label timestamps as datetime + clean_users <- dplyr::rename( + clean_users, + datetime_last_login = "last_login_date", + datetime_created_at = "created_at" + ) + + # clean up all character fields + clean_users <- dplyr::mutate( + .data = clean_users, + dplyr::across(dplyr::where(is.character), dplyr::na_if, "") + ) + + clean_users <- tidyr::unnest_wider(clean_users, "role_ids", names_sep = "_") + + # translate responses of categorical vars so easier to read + clean_users <- translate_categories( + data = clean_users, + language_tokens = language_tokens + ) + + # organize order of vars, only bring in what we need, take away confusing vars + clean_users <- dplyr::select( + .data = clean_users, + "id", + "first_name", + "last_name", + "email", + "institution_name", + "disregard_geographic_restrictions", + dplyr::starts_with("role_ids"), + "active_outbreak_id", + "created_by", + "datetime_created_at", + "datetime_last_login" + ) + + return(clean_users) +} diff --git a/R/contacts_per_case.R b/R/contacts_per_case.R new file mode 100644 index 0000000..cd32caa --- /dev/null +++ b/R/contacts_per_case.R @@ -0,0 +1,49 @@ +#' Counts the number of contacts per case from relationship data +#' +#' @description Uses cleaned relationship data to tally the number of contacts +#' per case. Relationship data is returned by [`get_relationships()`] and +#' cleaned by [`clean_relationships()`]. +#' +#' @param relationships_clean A `tibble` with the cleaned relationship data. +#' Relationship data is returned by [`get_relationships()`] and cleaned by +#' [`clean_relationships()`]. +#' +#' @return A `tibble` with the number of contacts associated to each source +#' person +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' relationships <- get_relationships( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' clean_relationships <- clean_relationships(relationships) +#' +#' contacts_per_case <- contacts_per_case(clean_relationships) +#' } +contacts_per_case <- function(relationships_clean) { + + contacts_per_case <- dplyr::group_by( + .data = relationships_clean, + .data$source_person_id + ) + + contacts_per_case <- dplyr::tally(x = contacts_per_case) + + contacts_per_case <- dplyr::select( + .data = contacts_per_case, + "source_person_id", + no_contacts = n + ) + + return(contacts_per_case) +} diff --git a/R/export_downloader.R b/R/export_downloader.R index 8bfa28a..2c19c4f 100644 --- a/R/export_downloader.R +++ b/R/export_downloader.R @@ -2,15 +2,21 @@ #' #' A housekeeping function to do export requests & downloads. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' @param api_call_request The API url to get the number of records. -#' @param wait The number of seconds to wait in between iterations of checking the status of the export. -#' @param file.type Whether the resulting data frame should contain nested fields (`file.type="json"`, the default) or an entirely flat data structure (`file.type="csv"`) - +#' @param wait The number of seconds to wait in between iterations of checking +#' the status of the export. +#' @param file_type Whether the resulting data frame should contain nested +#' fields (`file_type = "json"`, the default) or an entirely flat data structure +#' (`file_type = "csv"`) +#' #' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' `\link[tidyr]{nest}` for assitance with unnesting. #' #' @examples #' \dontrun{ @@ -19,66 +25,127 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' cases <- get_cases(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck -#' @importFrom utils read.csv -#' -#' -export_downloader <- function(url = url, - username = username, - password = password, - api_call_request = api_call_request, - wait = wait, - file.type = file.type) { +export_downloader <- function(url, + username, + password, + api_call_request, + wait, + file_type) { - request_id <- GET(paste0(api_call_request, - "?filter=%7B%22where%22%3A%7B%22useDbColumns%22%3A%22true%22%2C%20%22dontTranslateValues%22%3A%22true%22%2C%20%22jsonReplaceUndefinedWithNull%22%3A%22true%22%20%7D%7D", - "&type=",file.type, - "&access_token=",get_access_token(url=url, username=username, password=password))) %>% - content() %>% - pluck("exportLogId") + export_log_id_request <- httr::GET( + paste0( + api_call_request, + "?filter=%7B%22where%22%3A%7B%22useDbColumns%22%3A%22true%22%2C%20%22", + "dontTranslateValues%22%3A%22true%22%2C%20%22", + "jsonReplaceUndefinedWithNull%22%3A%22true%22%20%7D%7D", + "&type=", + file_type, + "&access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + export_log_id_request_content <- httr::content(export_log_id_request) + + request_id <- purrr::pluck(export_log_id_request_content, "exportLogId") #Check status of request periodcially, until finished - #function argument 'wait' determines the number of seconds to wait between iterations - export.request.status <- get_export_status(url=url, username=username, password=password, request_id=request_id) + #function argument 'wait' determines the number of seconds to wait between + #iterations + export_request_status <- get_export_status( + url = url, + username = username, + password = password, + request_id = request_id + ) - while(export.request.status$statusStep != "LNG_STATUS_STEP_EXPORT_FINISHED") { + status_step <- export_request_status$statusStep + while (status_step != "LNG_STATUS_STEP_EXPORT_FINISHED") { Sys.sleep(wait) - export.request.status <- GET(paste0(url,"api/export-logs/",request_id,"?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content() - message(paste0("...processed ",export.request.status$processedNo, " of ", export.request.status$totalNo, " records")) + export_request_status <- httr::GET( + paste0( + url, + "api/export-logs/", + request_id, + "?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + + export_request_status <- httr::content(export_request_status) + message( + paste0( + "...processed ", + export_request_status$processedNo, + " of ", + export_request_status$totalNo, + " records" + ) + ) } #Download the export message("...beginning download") - if (file.type=="json") { - df <- GET(paste0(url,"api/export-logs/",request_id,"/download?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content("text", encoding="UTF-8") %>% - fromJSON(flatten=TRUE) + if (file_type == "json") { + df_request <- httr::GET( + paste0( + url, + "api/export-logs/", + request_id, + "/download?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + + df_content <- httr::content(df_request, "text", encoding = "UTF-8") + + df <- jsonlite::fromJSON(df_content, flatten = TRUE) # fix one strange variable name names(df)[names(df) %in% "_id"] <- "id" - } else if (file.type=="csv") { - df <- GET(paste0(url,"api/export-logs/",request_id,"/download?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content("text", encoding="UTF-8") %>% - textConnection() %>% - read.csv() + } else if (file_type == "csv") { + df_request <- httr::GET( + paste0( + url, + "api/export-logs/", + request_id, + "/download?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + + df_content <- httr::content(df_request, "text", encoding = "UTF-8") + + df_content <- textConnection(df_content) + + df <- utils::read.csv(df_content) names(df)[names(df) %in% "X_id"] <- "id" } message("...download complete!") + df <- tibble::as_tibble(df) return(df) } - - diff --git a/R/exposures_per_case.R b/R/exposures_per_case.R new file mode 100644 index 0000000..a2ef7f2 --- /dev/null +++ b/R/exposures_per_case.R @@ -0,0 +1,49 @@ +#' Counts the number of exposures per case from relationship data +#' +#' @description Uses cleaned relationship data to tally the number of contacts +#' per case. Relationship data is returned by [`get_relationships()`] and +#' cleaned by [`clean_relationships()`]. +#' +#' @param relationships_clean A `tibble` with the cleaned relationship data. +#' Relationship data is returned by [`get_relationships()`] and cleaned by +#' [`clean_relationships()`]. +#' +#' @return A `tibble` with the number of exposures associated to each target +#' person +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' relationships <- get_relationships( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' clean_relationships <- clean_relationships(relationships) +#' +#' exposures_per_case <- exposures_per_case(clean_relationships) +#' } +exposures_per_case <- function(relationships_clean) { + + exposures_per_case <- dplyr::group_by( + .data = relationships_clean, + .data$target_person_id + ) + + exposures_per_case <- dplyr::tally(x = exposures_per_case) + + exposures_per_case <- dplyr::select( + .data = exposures_per_case, + "target_person_id", + no_exposures = n + ) + + return(exposures_per_case) +} diff --git a/R/get_access_token.R b/R/get_access_token.R index 73bff56..2b055cc 100644 --- a/R/get_access_token.R +++ b/R/get_access_token.R @@ -5,7 +5,8 @@ #' function used in many of the other #' `godataR` functions. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' @@ -20,24 +21,22 @@ #' username = username, #' password = password) #' } -#' @import httr -#' @importFrom jsonlite fromJSON #' @export +get_access_token <- function(url, + username, + password) { -get_access_token <- function(url=url, - username=username, - password=password) { + response <- httr::POST( + url = paste0(url, "api/oauth/token?access_token=123"), + body = list(username = username, password = password), + encode = "json" + ) - response <- POST(url=paste0(url,"api/oauth/token?access_token=123"), - body = list(username=username, password=password), - encode = "json") - - if (response$status_code==200) { - responseJSON <- content(response, as="text") - token <- fromJSON(responseJSON, flatten=TRUE)$access_token + if (response$status_code == 200) { + response_json <- httr::content(response, as = "text") + token <- jsonlite::fromJSON(response_json, flatten = TRUE)$access_token return(token) } else { - stop(paste0("Error: ",response$status_code)) + stop("Error: ", response$status_code) } - } diff --git a/R/get_active_outbreak.R b/R/get_active_outbreak.R index a87b6a1..7cd5420 100644 --- a/R/get_active_outbreak.R +++ b/R/get_active_outbreak.R @@ -7,7 +7,8 @@ #' housekeeping function used in many of the #' other `godataR` functions. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login. #' @@ -20,27 +21,38 @@ #' username <- "myemail@email.com" #' password <- "mypassword" #' -#' active_outbreak_id <- get_active_outbreak(url=url, -#' username=username, -#' password=password) +#' active_outbreak_id <- get_active_outbreak( +#' url = url, +#' username = username, +#' password = password +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON +get_active_outbreak <- function(url, + username, + password) { -get_active_outbreak <- function(url=url, - username=username, - password=password) { + # get request to go.data + godata_url <- httr::GET( + paste0( + url, + "api/users", + "?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) - users <- GET(paste0(url,"api/users", - "?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) + # unpack request as character string + url_content <- httr::content(godata_url, as = "text") - active.outbreak <- users$activeOutbreakId[users$email==username] + # converts JSON string into data frame + users <- jsonlite::fromJSON(url_content, flatten = TRUE) - return(active.outbreak) + # subset to active user + active_outbreak <- users$activeOutbreakId[users$email == username] + return(active_outbreak) } diff --git a/R/get_all_outbreaks.R b/R/get_all_outbreaks.R index ca98809..18c9639 100644 --- a/R/get_all_outbreaks.R +++ b/R/get_all_outbreaks.R @@ -5,12 +5,15 @@ #' housekeeping function used in many of the #' other `godataR` functions. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' #' @return -#' Returns data frame of outbreaks. The resulting list is filtered by the user's permissions: only outbreaks for which the user has access will be returned. +#' Returns data frame of outbreaks. The resulting list is filtered by the +#' user's permissions: only outbreaks for which the user has access will be +#' returned. #' @export #' @examples #' \dontrun{ @@ -18,29 +21,42 @@ #' username <- "myemail@email.com" #' password <- "mypassword" #' -#' outbreaks <- get_all_outbreaks(url=url, -#' username=username, -#' password=password) +#' outbreaks <- get_all_outbreaks( +#' url = url, +#' username = username, +#' password = password +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck #' @export +get_all_outbreaks <- function(url, + username, + password) { -get_all_outbreaks <- function(url=url, - username=username, - password=password) { + outbreaks_request <- httr::GET( + paste0( + url, + "api/outbreaks", + "?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) - outbreaks <- GET(paste0(url,"api/outbreaks", - "?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - filter(deleted!=TRUE) %>% - select(any_of(c("id", "name", "description","createdBy","createdAt"))) + outbreaks_content <- httr::content(outbreaks_request, as = "text") - return(outbreaks) + outbreaks <- jsonlite::fromJSON(outbreaks_content, flatten = TRUE) + + outbreaks <- dplyr::filter(outbreaks, .data$deleted != TRUE) + + outbreaks <- dplyr::select( + outbreaks, + dplyr::any_of(c("id", "name", "description", "createdBy", "createdAt")) + ) + outbreaks <- tibble::as_tibble(outbreaks) + + return(outbreaks) } diff --git a/R/get_cases.R b/R/get_cases.R index 41b8e39..b6bbbd9 100644 --- a/R/get_cases.R +++ b/R/get_cases.R @@ -10,7 +10,7 @@ #' Go.Data. This method relies on the GET outbreak/{id}/cases #' API endpoint. Records are then retrieved in batches #' based on `batch_size` and appended together into -#' a final dataset. `method="batches"` will be the default and +#' a final dataset. `method = "batches"` will be the default and #' only available method for Go.Data version 2.38.0 or older. #' #' `method="export"` will only work on Go.Data versions @@ -18,21 +18,31 @@ #' outbreak/{id}/cases/export API endpoint. An export #' request is submitted to the server, and then when the #' export is ready, it will be downloaded. Due to better -#' performance and more options, `method="export"` will +#' performance and more options, `method = "export"` will #' be the default if you are using Go.Data version 2.38.1 #' or newer. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. +#' Don't forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login -#' @param outbreak_id The id number for the outbreak for which you want to download data. -#' @param method The method to download data. `method="export"` is the preferred and default method for Go.Data version 2.38.1 or later. See Details. -#' @param batch_size If `method="batches"`, then `batch_size` specifies the number of records to retrieve in each iteration. -#' @param wait If `method="export"`, then `wait` is the number of seconds to wait in between iterations of checking the status of the export. -#' @param file.type If `method="export"`, then `file.type` determines Whether the resulting data frame should contain nested fields (`file.type="json"`, the default) or an entirely flat data structure (`file.type="csv"`) +#' @param outbreak_id The id number for the outbreak for which you want to +#' download data. +#' @param method The method to download data. `method = "export"` is the +#' preferred and default method for Go.Data version 2.38.1 or later. +#' See Details. +#' @param batch_size If `method = "batches"`, then `batch_size` specifies the +#' number of records to retrieve in each iteration. +#' @param wait If `method = "export"`, then `wait` is the number of seconds to +#' wait in between iterations of checking the status of the export. +#' @param file_type If `method = "export"`, then `file_type` determines Whether +#' the resulting data frame should contain nested fields (`file_type = "json"`, +#' the default) or an entirely flat data structure (`file_type = "csv"`) #' #' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' `\link[tidyr]{nest}` for assitance with unnesting. #' @export #' #' @examples @@ -42,37 +52,40 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' cases <- get_cases(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck - - -get_cases <- function(url=url, - username=username, - password=password, - outbreak_id=outbreak_id, - method=c("export","batch"), - batch_size=50000, - wait=2, - file.type=c("json","csv")) { +get_cases <- function(url, + username, + password, + outbreak_id, + method = c("export", "batch"), + batch_size = 50000, + wait = 2, + file_type = c("json", "csv")) { #Check that outbreak_id is active - if (outbreak_id != get_active_outbreak(url=url, username=username, password=password)) { - set_active_outbreak(url=url, username=username, password=password, outbreak_id=outbreak_id) + active_outbreak_id <- get_active_outbreak( + url = url, + username = username, + password = password + ) + if (outbreak_id != active_outbreak_id) { + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) } #Set default method based on current version of Go.Data - version.check <- check_godata_version(url=url) - if (!version.check) { + version_check <- check_godata_version(url = url) + if (!version_check) { method <- "batches" #Older version of Go.Data can only use the batch method } else if (missing(method)) { method <- "export" # For new versions of Go.Data, default to export method @@ -81,31 +94,34 @@ get_cases <- function(url=url, if (method == "batches") { - api_call_n <- paste0(url, "api/outbreaks/",outbreak_id,"/cases/count") - api_call_get <- paste0(url, "api/outbreaks/",outbreak_id,"/cases") - df <- batch_downloader(url=url, - username=username, - password=password, - api_call_n=api_call_n, - api_call_get=api_call_get, - batch_size=batch_size) + api_call_n <- paste0(url, "api/outbreaks/", outbreak_id, "/cases/count") + api_call_get <- paste0(url, "api/outbreaks/", outbreak_id, "/cases") + df <- batch_downloader( + url = url, + username = username, + password = password, + api_call_n = api_call_n, + api_call_get = api_call_get, + batch_size = batch_size + ) } else if (method == "export") { - #Default value of file.type is "json" - if (missing(file.type)) file.type <- "json" + # Default value of file_type is "json" + if (missing(file_type)) file_type <- "json" #Submit an export request to the system - api_call_request <- paste0(url,"api/outbreaks/",outbreak_id,"/cases/export") - df <- export_downloader(url=url, - username=username, - password=password, - api_call_request=api_call_request, - file.type = file.type, - wait = wait) - - + api_call_request <- paste0( + url, "api/outbreaks/", outbreak_id, "/cases/export" + ) + df <- export_downloader( + url = url, + username = username, + password = password, + api_call_request = api_call_request, + file_type = file_type, + wait = wait + ) } - - return(df) + return(tibble::as_tibble(df)) } diff --git a/R/get_cases_questionnaire.R b/R/get_cases_questionnaire.R new file mode 100644 index 0000000..2ba125d --- /dev/null +++ b/R/get_cases_questionnaire.R @@ -0,0 +1,78 @@ +#' Download cases from Go.Data and returns questionnaire fields +#' +#' A function that retrieves the questionnaire fields from case data for a +#' specific `outbreak_id`. +#' +#' Unlike [`get_cases()`] this function only uses the [`export_downloader()`], +#' and not the [`batch_downloader()`]. Therefore, this function will only work +#' on Go.Data versions 2.38.1 or newer. This method relies on the GET +#' outbreak/{id}/cases/export API endpoint. An export request is submitted to +#' the server, and then when the export is ready, it will be downloaded. +#' +#' This function fixes the file return type to `"csv"`. +#' +#' @param url Insert the base URL for your instance of Go.Data here. +#' Don't forget the forward slash "/" at end! +#' @param username The email address for your Go.Data login. +#' @param password The password for your Go.Data login +#' @param outbreak_id The id number for the outbreak for which you want to +#' download data. +#' @param wait If `method = "export"`, then `wait` is the number of seconds to +#' wait in between iterations of checking the status of the export. +#' +#' @return Returns a `tibble`. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' cases <- get_cases_questionnaire( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' } +get_cases_questionnaire <- function(url, + username, + password, + outbreak_id, + wait = 2) { + + #Check that outbreak_id is active + active_outbreak_id <- get_active_outbreak( + url = url, + username = username, + password = password + ) + if (outbreak_id != active_outbreak_id) { + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + } + + #Submit an export request to the system + api_call_request <- paste0( + url, "api/outbreaks/", outbreak_id, "/cases/export" + ) + df <- export_downloader( + url = url, + username = username, + password = password, + api_call_request = api_call_request, + file_type = "csv", + wait = wait + ) + + # subset columns to questionnaire + df_questionnaire <- df[, grep(pattern = "FA", x = colnames(df))] + + return(tibble::as_tibble(df_questionnaire)) +} diff --git a/R/get_clusters.R b/R/get_clusters.R index fecc251..dbbd548 100644 --- a/R/get_clusters.R +++ b/R/get_clusters.R @@ -9,11 +9,14 @@ #' #' This function works on all versions of Go.Data. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login -#' @param outbreak_id The id number for the outbreak for which you want to download clusters. -#' @param batch_size For large datasets, specify the number of records to retrieve in each iteration. +#' @param outbreak_id The id number for the outbreak for which you want to +#' download clusters. +#' @param batch_size For large datasets, specify the number of records to +#' retrieve in each iteration. #' #' @return #' Returns data frame. @@ -26,68 +29,104 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' clusters <- get_clusters(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' clusters <- get_clusters( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck - -get_clusters <- function(url=url, - username=username, - password=password, - outbreak_id=outbreak_id, - batch_size=50000) { +get_clusters <- function(url, + username, + password, + outbreak_id, + batch_size = 50000) { # no /export endpoint for clusters so no need to check version + outbreak_id_api <- get_active_outbreak( + url = url, + username = username, + password = password + ) #Check that outbreak_id is active - if (outbreak_id != get_active_outbreak(url=url, username=username, password=password)) { - set_active_outbreak(url=url, username=username, password=password, outbreak_id=outbreak_id) + if (outbreak_id != outbreak_id_api) { + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) } #get total number of records - df_n <- GET(paste0(url,"api/outbreaks/",outbreak_id,"/clusters/count"), - add_headers(Authorization = paste("Bearer", get_access_token(url=url, username=username, password=password), sep = " "))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - unlist() %>% - unname() + df_n_request <- httr::GET( + paste0(url, "api/outbreaks/", outbreak_id, "/clusters/count"), + httr::add_headers(Authorization = paste("Bearer", get_access_token( + url = url, + username = username, + password = password + ), sep = " "))) + + df_n_content <- httr::content(df_n_request, as = "text") + + df_n <- jsonlite::fromJSON(df_n_content, flatten = TRUE) + df_n <- unname(unlist(df_n)) #Import records in batches - df <- tibble() + df <- tibble::tibble() batch_size <- batch_size # number of records to import per iteration - skip <-0 + skip <- 0 message("****************************") #Download records in batches, and then append them into a single dataset while (skip < df_n) { #Progress message - if (df_n <= batch_size) message(paste0("...downloading records 1 to ",df_n)) - if (df_n > batch_size) message(paste0("...downloading records ", as.character(skip+1, scientific = FALSE), " to ", format(skip+batch_size, scientific = FALSE))) + if (df_n <= batch_size) { + message(paste0("...downloading records 1 to ", df_n)) + } + if (df_n > batch_size) { + message(paste0( + "...downloading records ", + as.character(skip + 1, scientific = FALSE), + " to ", + format(skip + batch_size, scientific = FALSE) + )) + } #fetch the batch of records - df.i <- GET(paste0(url,"api/outbreaks/",outbreak_id,"/clusters", - "/?filter={%22limit%22:",format(batch_size, scientific = FALSE),",%22skip%22:",format(skip, scientific = FALSE),"}"), - add_headers(Authorization = paste("Bearer", get_access_token(url=url, username=username, password=password), sep = " "))) %>% - content(as='text') %>% - fromJSON( flatten=TRUE) %>% - as_tibble() + df_i_request <- httr::GET( + paste0( + url, + "api/outbreaks/", + outbreak_id, + "/clusters", + "/?filter={%22limit%22:", + format(batch_size, scientific = FALSE), + ",%22skip%22:", + format(skip, scientific = FALSE), + "}" + ), + httr::add_headers(Authorization = paste("Bearer", get_access_token( + url = url, + username = username, + password = password + ), sep = " ")) + ) + + df_i_content <- httr::content(df_i_request, as = "text") + + df_i <- jsonlite::fromJSON(df_i_content, flatten = TRUE) + + df_i <- tibble::as_tibble(df_i) #append the new batch of records to the existing data frame - df <- df %>% - bind_rows(df.i) + df <- dplyr::bind_rows(df, df_i) #update numbers for the next iteration skip <- skip + batch_size - rm(df.i) + rm(df_i) } rm(batch_size, skip, df_n) diff --git a/R/get_contacts.R b/R/get_contacts.R index c79b1e3..3095f00 100644 --- a/R/get_contacts.R +++ b/R/get_contacts.R @@ -6,33 +6,43 @@ #' This function works on all versions of Go.Data. There #' are two methods for downloading the data: #' -#' `method="batches"` will work on all versions of +#' `method = "batches"` will work on all versions of #' Go.Data. This method relies on the GET outbreak/{id}/contacts #' API endpoint. Records are then retrieved in batches #' based on `batch_size` and appended together into -#' a final dataset. `method="batches"` will be the default and +#' a final dataset. `method = "batches"` will be the default and #' only available method for Go.Data version 2.38.0 or older. #' -#' `method="export"` will only work on Go.Data versions +#' `method = "export"` will only work on Go.Data versions #' 2.38.1 or newer. This method relies on the GET #' outbreak/{id}/contacts/export API endpoint. An export #' request is submitted to the server, and then when the #' export is ready, it will be downloaded. Due to better -#' performance and more options, `method="export"` will +#' performance and more options, `method = "export"` will #' be the default if you are using Go.Data version 2.38.1 #' or newer. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login -#' @param outbreak_id The id number for the outbreak for which you want to download data. -#' @param method The method to download data. `method="export"` is the preferred and default method for Go.Data version 2.38.1 or later. See Details. -#' @param batch_size If `method="batches"`, then `batch_size` specifies the number of records to retrieve in each iteration. -#' @param wait If `method="export"`, then `wait` is the number of seconds to wait in between iterations of checking the status of the export. -#' @param file.type If `method="export"`, then `file.type` determines Whether the resulting data frame should contain nested fields (`file.type="json"`, the default) or an entirely flat data structure (`file.type="csv"`) +#' @param outbreak_id The id number for the outbreak for which you want to +#' download data. +#' @param method The method to download data. `method="export"` is the +#' preferred and default method for Go.Data version 2.38.1 or later. See +#' Details. +#' @param batch_size If `method = "batches"`, then `batch_size` specifies the +#' number of records to retrieve in each iteration. +#' @param wait If `method = "export"`, then `wait` is the number of seconds to +#' wait in between iterations of checking the status of the export. +#' @param file_type If `method="export"`, then `file_type` determines Whether +#' the resulting data frame should contain nested fields (`file_type = "json"`, +#' the default) or an entirely flat data structure (`file_type = "csv"`) #' #' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' \code{\link[tidyr]{nest}} for assitance with unnesting. #' @export #' #' @examples @@ -42,37 +52,40 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' contacts <- get_contacts(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' contacts <- get_contacts( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id=outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck - - -get_contacts <- function(url=url, - username=username, - password=password, - outbreak_id=outbreak_id, - method=c("export","batch"), - batch_size=50000, - wait=2, - file.type=c("json","csv")) { +get_contacts <- function(url, + username, + password, + outbreak_id, + method = c("export", "batch"), + batch_size = 50000, + wait = 2, + file_type = c("json", "csv")) { + active_outbreak <- get_active_outbreak( + url = url, + username = username, + password = password + ) #Check that outbreak_id is active - if (outbreak_id != get_active_outbreak(url=url, username=username, password=password)) { - set_active_outbreak(url=url, username=username, password=password, outbreak_id=outbreak_id) + if (outbreak_id != active_outbreak) { + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) } #Set default method based on current version of Go.Data - version.check <- check_godata_version(url=url) - if (!version.check) { + version_check <- check_godata_version(url = url) + if (!version_check) { method <- "batches" #Older version of Go.Data can only use the batch method } else if (missing(method)) { method <- "export" # For new versions of Go.Data, default to export method @@ -81,31 +94,45 @@ get_contacts <- function(url=url, if (method == "batches") { - api_call_n <- paste0(url, "api/outbreaks/",outbreak_id,"/contacts/filtered-count") - api_call_get <- paste0(url, "api/outbreaks/",outbreak_id,"/contacts") - df <- batch_downloader(url=url, - username=username, - password=password, - api_call_n=api_call_n, - api_call_get=api_call_get, - batch_size=batch_size) + api_call_n <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/contacts/filtered-count" + ) + api_call_get <- paste0(url, "api/outbreaks/", outbreak_id, "/contacts") + df <- batch_downloader( + url = url, + username = username, + password = password, + api_call_n = api_call_n, + api_call_get = api_call_get, + batch_size = batch_size + ) } else if (method == "export") { - #Default value of file.type is "json" - if (missing(file.type)) file.type <- "json" + #Default value of file_type is "json" + if (missing(file_type)) file_type <- "json" #Submit an export request to the system - api_call_request <- paste0(url,"api/outbreaks/",outbreak_id,"/contacts/export") - df <- export_downloader(url=url, - username=username, - password=password, - api_call_request=api_call_request, - file.type = file.type, - wait = wait) - - + api_call_request <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/contacts/export" + ) + df <- export_downloader( + url = url, + username = username, + password = password, + api_call_request = api_call_request, + file_type = file_type, + wait = wait + ) } + df <- tibble::as_tibble(df) + return(df) } diff --git a/R/get_contacts_of_contacts.R b/R/get_contacts_of_contacts.R index 7459ffb..a18baae 100644 --- a/R/get_contacts_of_contacts.R +++ b/R/get_contacts_of_contacts.R @@ -6,34 +6,44 @@ #' This function works on all versions of Go.Data. There #' are two methods for downloading the data: #' -#' `method="batches"` will work on all versions of +#' `method = "batches"` will work on all versions of #' Go.Data. This method relies on the GET #' outbreak/{id}/contacts-of-contacts API endpoint. #' Records are then retrieved in batches based on #' `batch_size` and appended together into a final -#' dataset. `method="batches"` will be the default and +#' dataset. `method = "batches"` will be the default and #' only available method for Go.Data version 2.38.0 or older. #' -#' `method="export"` will only work on Go.Data versions +#' `method = "export"` will only work on Go.Data versions #' 2.38.1 or newer. This method relies on the GET #' outbreak/{id}/contacts-of-contacts/export API endpoint. #' An export request is submitted to the server, and then #' when the export is ready, it will be downloaded. Due to -#' better performance and more options, `method="export"` will +#' better performance and more options, `method = "export"` will #' be the default if you are using Go.Data version 2.38.1 #' or newer. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login -#' @param outbreak_id The id number for the outbreak for which you want to download data. -#' @param method The method to download data. `method="export"` is the preferred and default method for Go.Data version 2.38.1 or later. See Details. -#' @param batch_size If `method="batches"`, then `batch_size` specifies the number of records to retrieve in each iteration. -#' @param wait If `method="export"`, then `wait` is the number of seconds to wait in between iterations of checking the status of the export. -#' @param file.type If `method="export"`, then `file.type` determines Whether the resulting data frame should contain nested fields (`file.type="json"`, the default) or an entirely flat data structure (`file.type="csv"`) +#' @param outbreak_id The id number for the outbreak for which you want to +#' download data. +#' @param method The method to download data. `method = "export"` is the +#' preferred and default method for Go.Data version 2.38.1 or later. See +#' Details. +#' @param batch_size If `method = "batches"`, then `batch_size` specifies the +#' number of records to retrieve in each iteration. +#' @param wait If `method = "export"`, then `wait` is the number of seconds to +#' wait in between iterations of checking the status of the export. +#' @param file_type If `method = "export"`, then `file_type` determines Whether +#' the resulting data frame should contain nested fields (`file_type = "json"`, +#' the default) or an entirely flat data structure (`file_type = "csv"`) #' #' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' \code{\link[tidyr]{nest}} for assitance with unnesting. #' @export #' #' @examples @@ -43,37 +53,40 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' contacts_of_contacts <- get_contacts_of_contacts(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' contacts_of_contacts <- get_contacts_of_contacts( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck - - -get_contacts_of_contacts <- function(url=url, - username=username, - password=password, - outbreak_id=outbreak_id, - method=c("export","batch"), - batch_size=50000, - wait=2, - file.type=c("json","csv")) { +get_contacts_of_contacts <- function(url, + username, + password, + outbreak_id, + method = c("export", "batch"), + batch_size = 50000, + wait = 2, + file_type = c("json", "csv")) { #Check that outbreak_id is active - if (outbreak_id != get_active_outbreak(url=url, username=username, password=password)) { - set_active_outbreak(url=url, username=username, password=password, outbreak_id=outbreak_id) + active_outbreak <- get_active_outbreak( + url = url, + username = username, + password = password + ) + if (outbreak_id != active_outbreak) { + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) } #Set default method based on current version of Go.Data - version.check <- check_godata_version(url=url) - if (!version.check) { + version_check <- check_godata_version(url = url) + if (!version_check) { method <- "batches" #Older version of Go.Data can only use the batch method } else if (missing(method)) { method <- "export" # For new versions of Go.Data, default to export method @@ -82,31 +95,50 @@ get_contacts_of_contacts <- function(url=url, if (method == "batches") { - api_call_n <- paste0(url, "api/outbreaks/",outbreak_id,"/contacts-of-contacts/filtered-count") - api_call_get <- paste0(url, "api/outbreaks/",outbreak_id,"/contacts-of-contacts") - df <- batch_downloader(url=url, - username=username, - password=password, - api_call_n=api_call_n, - api_call_get=api_call_get, - batch_size=batch_size) + api_call_n <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/contacts-of-contacts/filtered-count" + ) + api_call_get <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/contacts-of-contacts" + ) + df <- batch_downloader( + url = url, + username = username, + password = password, + api_call_n = api_call_n, + api_call_get = api_call_get, + batch_size = batch_size + ) } else if (method == "export") { - #Default value of file.type is "json" - if (missing(file.type)) file.type <- "json" + #Default value of file_type is "json" + if (missing(file_type)) file_type <- "json" #Submit an export request to the system - api_call_request <- paste0(url,"api/outbreaks/",outbreak_id,"/contacts-of-contacts/export") - df <- export_downloader(url=url, - username=username, - password=password, - api_call_request=api_call_request, - file.type = file.type, - wait = wait) - - + api_call_request <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/contacts-of-contacts/export" + ) + df <- export_downloader( + url = url, + username = username, + password = password, + api_call_request = api_call_request, + file_type = file_type, + wait = wait + ) } + df <- tibble::as_tibble(df) + return(df) } diff --git a/R/get_date_range.R b/R/get_date_range.R index de11e1b..3597c38 100644 --- a/R/get_date_range.R +++ b/R/get_date_range.R @@ -1,59 +1,58 @@ -#' Get minimum and maximum date for a vector of dates -#' +#' Get minimum and maximum date for a vector of dates +#' #' @author Amy Mikhail, \email{amy.mikhail@@gmail.com} -#' -#' @description -#' This function will attempt to auto-detect the date format using lubridate, -#' then determine the minimum and maximum for the submitted vector of dates. +#' +#' @description +#' This function will attempt to auto-detect the date format using lubridate, +#' then determine the minimum and maximum for the submitted vector of dates. #' It is intended as a helper function for selecting date ranges and adding #' them to queries. -#' +#' #' @md -#' +#' #' @param dates character vector of dates to extract the date range from -#' -#' @return -#' Returns a list object with two values (minimum and maximum date) -#' +#' +#' @return +#' Returns a list object with two values (minimum and maximum date) +#' #' @import lubridate -#' -#' @examples +#' +#' @examples #' # Create a character vector of dates: #' x <- c("2022-07-15", "2021-08-09", NA, "2022-08-03") -#' +#' #' # Get date range: #' daterange <- get_date_range(dates = x) -#' +#' #' # View the result: #' daterange #' @export -get_date_range <- function(dates){ - +get_date_range <- function(dates) { + # Check if the input variable is already in date format: - if(any(class(dates) %in% c("Date", "POSIXt"))){ - + if (any(class(dates) %in% c("Date", "POSIXt"))) { + # If yes, no need to format: - dates2range = dates - + dates2range <- dates + } else { - + # If no, convert to date format with lubridate: - dates2range = lubridate::parse_date_time(x = dates, - orders = c("ymd", - "dmy", - "mdy")) + dates2range <- lubridate::parse_date_time( + x = dates, + orders = c("ymd", "dmy", "mdy") + ) } - + # Get the minimum date: - mindate = min(dates2range, na.rm = TRUE) - + mindate <- min(dates2range, na.rm = TRUE) + # Get the maximum date: - maxdate = max(dates2range, na.rm = TRUE) - + maxdate <- max(dates2range, na.rm = TRUE) + # Compile results: - daterange = list(mindate = mindate, maxdate = maxdate) - + daterange <- list(mindate = mindate, maxdate = maxdate) + # Return the results: return(daterange) - -} \ No newline at end of file +} diff --git a/R/get_events.R b/R/get_events.R index 88f474f..cbb2a29 100644 --- a/R/get_events.R +++ b/R/get_events.R @@ -1,111 +1,140 @@ -#' Download events from Go.Data -#' -#' A function to retrieve the event data for a -#' specific `outbreak_id`. -#' -#' This function works on all versions of Go.Data. There -#' are two methods for downloading the data: -#' -#' `method="batches"` will work on all versions of -#' Go.Data. This method relies on the GET outbreak/{id}/events -#' API endpoint. Records are then retrieved in batches -#' based on `batch_size` and appended together into -#' a final dataset. `method="batches"` will be the default and -#' only available method for Go.Data version 2.38.0 or older. -#' -#' `method="export"` will only work on Go.Data versions -#' 2.38.1 or newer. This method relies on the GET -#' outbreak/{id}/events/export API endpoint. An export -#' request is submitted to the server, and then when the -#' export is ready, it will be downloaded. Due to better -#' performance and more options, `method="export"` will -#' be the default if you are using Go.Data version 2.38.1 -#' or newer. -#' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! -#' @param username The email address for your Go.Data login. -#' @param password The password for your Go.Data login -#' @param outbreak_id The id number for the outbreak for which you want to download data. -#' @param method The method to download data. `method="export"` is the preferred and default method for Go.Data version 2.38.1 or later. See Details. -#' @param batch_size If `method="batches"`, then `batch_size` specifies the number of records to retrieve in each iteration. -#' @param wait If `method="export"`, then `wait` is the number of seconds to wait in between iterations of checking the status of the export. -#' @param file.type If `method="export"`, then `file.type` determines Whether the resulting data frame should contain nested fields (`file.type="json"`, the default) or an entirely flat data structure (`file.type="csv"`) -#' -#' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. -#' @export -#' -#' @examples -#' \dontrun{ -#' url <- "https://MyGoDataServer.com/" -#' username <- "myemail@email.com" -#' password <- "mypassword" -#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -#' -#' events <- get_events(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) -#' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck - - -get_events <- function(url=url, - username=username, - password=password, - outbreak_id=outbreak_id, - method=c("export","batch"), - batch_size=50000, - wait=2, - file.type=c("json","csv")) { - - #Check that outbreak_id is active - if (outbreak_id != get_active_outbreak(url=url, username=username, password=password)) { - set_active_outbreak(url=url, username=username, password=password, outbreak_id=outbreak_id) - } - - #Set default method based on current version of Go.Data - version.check <- check_godata_version(url=url) - if (!version.check) { - method <- "batches" #Older version of Go.Data can only use the batch method - } else if (missing(method)) { - method <- "export" # For new versions of Go.Data, default to export method - } - - - if (method == "batches") { - - api_call_n <- paste0(url, "api/outbreaks/",outbreak_id,"/events/filtered-count") - api_call_get <- paste0(url, "api/outbreaks/",outbreak_id,"/events") - df <- batch_downloader(url=url, - username=username, - password=password, - api_call_n=api_call_n, - api_call_get=api_call_get, - batch_size=batch_size) - - } else if (method == "export") { - - #Default value of file.type is "json" - if (missing(file.type)) file.type <- "json" - - #Submit an export request to the system - api_call_request <- paste0(url,"api/outbreaks/",outbreak_id,"/events/export") - df <- export_downloader(url=url, - username=username, - password=password, - api_call_request=api_call_request, - file.type = file.type, - wait = wait) - - - } - - return(df) -} +#' Download events from Go.Data +#' +#' A function to retrieve the event data for a +#' specific `outbreak_id`. +#' +#' This function works on all versions of Go.Data. There +#' are two methods for downloading the data: +#' +#' `method = "batches"` will work on all versions of +#' Go.Data. This method relies on the GET outbreak/{id}/events +#' API endpoint. Records are then retrieved in batches +#' based on `batch_size` and appended together into +#' a final dataset. `method = "batches"` will be the default and +#' only available method for Go.Data version 2.38.0 or older. +#' +#' `method = "export"` will only work on Go.Data versions +#' 2.38.1 or newer. This method relies on the GET +#' outbreak/{id}/events/export API endpoint. An export +#' request is submitted to the server, and then when the +#' export is ready, it will be downloaded. Due to better +#' performance and more options, `method = "export"` will +#' be the default if you are using Go.Data version 2.38.1 +#' or newer. +#' +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! +#' @param username The email address for your Go.Data login. +#' @param password The password for your Go.Data login +#' @param outbreak_id The id number for the outbreak for which you want to +#' download data. +#' @param method The method to download data. `method = "export"` is the +#' preferred and default method for Go.Data version 2.38.1 or later. See +#' Details. +#' @param batch_size If `method = "batches"`, then `batch_size` specifies the +#' number of records to retrieve in each iteration. +#' @param wait If `method = "export"`, then `wait` is the number of seconds to +#' wait in between iterations of checking the status of the export. +#' @param file_type If `method = "export"`, then `file_type` determines Whether +#' the resulting data frame should contain nested fields (`file_type = "json"`, +#' the default) or an entirely flat data structure (`file_type = "csv"`) +#' +#' @return +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' \code{\link[tidyr]{nest}} for assitance with unnesting. +#' @export +#' +#' @examples +#' \dontrun{ +#' url <- "https://MyGoDataServer.com/" +#' username <- "myemail@email.com" +#' password <- "mypassword" +#' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" +#' +#' events <- get_events( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' } +get_events <- function(url, + username, + password, + outbreak_id, + method = c("export", "batch"), + batch_size = 50000, + wait = 2, + file_type = c("json", "csv")) { + + #Check that outbreak_id is active + active_outbreak <- get_active_outbreak( + url = url, + username = username, + password = password + ) + if (outbreak_id != active_outbreak) { + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + } + + #Set default method based on current version of Go.Data + version_check <- check_godata_version(url = url) + if (!version_check) { + method <- "batches" #Older version of Go.Data can only use the batch method + } else if (missing(method)) { + method <- "export" # For new versions of Go.Data, default to export method + } + + + if (method == "batches") { + + api_call_n <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/events/filtered-count" + ) + api_call_get <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/events" + ) + df <- batch_downloader( + url = url, + username = username, + password = password, + api_call_n = api_call_n, + api_call_get = api_call_get, + batch_size = batch_size + ) + + } else if (method == "export") { + + #Default value of file_type is "json" + if (missing(file_type)) file_type <- "json" + + #Submit an export request to the system + api_call_request <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/events/export" + ) + df <- export_downloader( + url = url, + username = username, + password = password, + api_call_request = api_call_request, + file_type = file_type, + wait = wait + ) + } + return(tibble::as_tibble(df)) +} diff --git a/R/get_export_status.R b/R/get_export_status.R index 2daf647..a400b0c 100644 --- a/R/get_export_status.R +++ b/R/get_export_status.R @@ -4,7 +4,8 @@ #' request. This is a housekeeping function #' used in many of the other `godataR` functions. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' @param request_id The id number for the export request. @@ -18,41 +19,58 @@ #' username <- "myemail@email.com" #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -#' access_token <- get_access_token(url=url, -#' username=username, -#' password=password) +#' access_token <- get_access_token( +#' url = url, +#' username = username, +#' password = password +#' ) #' -#' #Submit an export request -#' export.request <- GET(paste0(url,"api/outbreaks/",outbreak_id,"/cases/export", -#' "&access_token=",access_token)) -#' request_id <- export.request %>% -#' content() %>% -#' pluck("exportLogId") +#' # Submit an export request +#' export_request <- GET( +#' paste0( +#' url, +#' "api/outbreaks/", +#' outbreak_id, +#' "/cases/export", +#' "&access_token=", +#' access_token +#' ) +#' ) +#' request_id <- content(export_request) +#' request_id <- pluck(request_id, "exportLogId") #' -#' #Check the status of the export request +#' # Check the status of the export request #' -#' export.request.status <- get_export_status(url=url, -#' username=username, -#' password=password, -#' request_id=request_id) +#' export_request_status <- get_export_status( +#' url = url, +#' username = username, +#' password = password, +#' request_id = request_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck +get_export_status <- function(url, + username, + password, + request_id) { -get_export_status <- function(url=url, - username=username, - password=password, - request_id=request_id) { + export_request_status <- httr::GET( + paste0( + url, + "api/export-logs/", + request_id, + "?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + export_request_status <- httr::content(export_request_status) - export.request.status <- GET(paste0(url,"api/export-logs/",request_id,"?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content() - - export.request.status <- export.request.status[c("statusStep","totalNo","processedNo")] - - return(export.request.status) + export_request_status <- export_request_status[ + c("statusStep", "totalNo", "processedNo") + ] + return(export_request_status) } diff --git a/R/get_followups.R b/R/get_followups.R index 731a860..6eccdb3 100644 --- a/R/get_followups.R +++ b/R/get_followups.R @@ -6,33 +6,43 @@ #' This function works on all versions of Go.Data. There #' are two methods for downloading the data: #' -#' `method="batches"` will work on all versions of +#' `method = "batches"` will work on all versions of #' Go.Data. This method relies on the GET outbreak/{id}/follow-ups #' API endpoint. Records are then retrieved in batches #' based on `batch_size` and appended together into -#' a final dataset. `method="batches"` will be the default and +#' a final dataset. `method = "batches"` will be the default and #' only available method for Go.Data version 2.38.0 or older. #' -#' `method="export"` will only work on Go.Data versions +#' `method = "export"` will only work on Go.Data versions #' 2.38.1 or newer. This method relies on the GET #' outbreak/{id}/follow-ups/export API endpoint. An export #' request is submitted to the server, and then when the #' export is ready, it will be downloaded. Due to better -#' performance and more options, `method="export"` will +#' performance and more options, `method = "export"` will #' be the default if you are using Go.Data version 2.38.1 #' or newer. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login -#' @param outbreak_id The id number for the outbreak for which you want to download data. -#' @param method The method to download data. `method="export"` is the preferred and default method for Go.Data version 2.38.1 or later. See Details. -#' @param batch_size If `method="batches"`, then `batch_size` specifies the number of records to retrieve in each iteration. -#' @param wait If `method="export"`, then `wait` is the number of seconds to wait in between iterations of checking the status of the export. -#' @param file.type If `method="export"`, then `file.type` determines Whether the resulting data frame should contain nested fields (`file.type="json"`, the default) or an entirely flat data structure (`file.type="csv"`) +#' @param outbreak_id The id number for the outbreak for which you want to +#' download data. +#' @param method The method to download data. `method = "export"` is the +#' preferred and default method for Go.Data version 2.38.1 or later. See +#' Details. +#' @param batch_size If `method = "batches"`, then `batch_size` specifies the +#' number of records to retrieve in each iteration. +#' @param wait If `method = "export"`, then `wait` is the number of seconds to +#' wait in between iterations of checking the status of the export. +#' @param file_type If `method = "export"`, then `file_type` determines Whether +#' the resulting data frame should contain nested fields (`file_type = "json"`, +#' the default) or an entirely flat data structure (`file_type = "csv"`) #' #' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' \code{\link[tidyr]{nest}} for assitance with unnesting. #' @export #' #' @examples @@ -42,37 +52,40 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' followups <- get_followups(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' followups <- get_followups( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck - - -get_followups <- function(url=url, - username=username, - password=password, - outbreak_id=outbreak_id, - method=c("export","batch"), - batch_size=50000, - wait=2, - file.type=c("json","csv")) { +get_followups <- function(url, + username, + password, + outbreak_id, + method = c("export", "batch"), + batch_size = 50000, + wait = 2, + file_type = c("json", "csv")) { #Check that outbreak_id is active - if (outbreak_id != get_active_outbreak(url=url, username=username, password=password)) { - set_active_outbreak(url=url, username=username, password=password, outbreak_id=outbreak_id) + active_outbreak <- get_active_outbreak( + url = url, + username = username, + password = password + ) + if (outbreak_id != active_outbreak) { + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) } #Set default method based on current version of Go.Data - version.check <- check_godata_version(url=url) - if (!version.check) { + version_check <- check_godata_version(url = url) + if (!version_check) { method <- "batches" #Older version of Go.Data can only use the batch method } else if (missing(method)) { method <- "export" # For new versions of Go.Data, default to export method @@ -81,31 +94,47 @@ get_followups <- function(url=url, if (method == "batches") { - api_call_n <- paste0(url, "api/outbreaks/",outbreak_id,"/follow-ups/filtered-count") - api_call_get <- paste0(url, "api/outbreaks/",outbreak_id,"/follow-ups") - df <- batch_downloader(url=url, - username=username, - password=password, - api_call_n=api_call_n, - api_call_get=api_call_get, - batch_size=batch_size) + api_call_n <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/follow-ups/filtered-count" + ) + api_call_get <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/follow-ups" + ) + df <- batch_downloader( + url = url, + username = username, + password = password, + api_call_n = api_call_n, + api_call_get = api_call_get, + batch_size = batch_size + ) } else if (method == "export") { - #Default value of file.type is "json" - if (missing(file.type)) file.type <- "json" + #Default value of file_type is "json" + if (missing(file_type)) file_type <- "json" #Submit an export request to the system - api_call_request <- paste0(url,"api/outbreaks/",outbreak_id,"/follow-ups/export") - df <- export_downloader(url=url, - username=username, - password=password, - api_call_request=api_call_request, - file.type = file.type, - wait = wait) - - + api_call_request <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/follow-ups/export" + ) + df <- export_downloader( + url = url, + username = username, + password = password, + api_call_request = api_call_request, + file_type = file_type, + wait = wait + ) } - - return(df) + return(tibble::as_tibble(df)) } diff --git a/R/get_godata_version.R b/R/get_godata_version.R index 0ae96e0..375cabc 100644 --- a/R/get_godata_version.R +++ b/R/get_godata_version.R @@ -5,7 +5,8 @@ #' housekeeping function used in many of the #' other `godataR` functions. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' #' @return string #' @export @@ -13,22 +14,17 @@ #' @examples #' \dontrun{ #' url <- "https://MyGoDataServer.com/" -#' get_godata_version(url=url) +#' get_godata_version(url = url) #' } -#' @import httr -#' @importFrom jsonlite fromJSON -#' @importFrom magrittr %>% +get_godata_version <- function(url = url) { -get_godata_version <- function(url=url) { + version_request <- httr::GET(paste0(url, "api/system-settings/version")) - version.request <- GET(paste0(url,"api/system-settings/version")) - - if (version.request$status_code==200) { - version <- content(version.request, as="text") %>% - fromJSON(flatten=TRUE) + if (version_request$status_code == 200) { + version <- httr::content(version_request, as = "text") + version <- jsonlite::fromJSON(version, flatten = TRUE) return(version$version) } else { - stop(paste0("Error ",version.request$status_code)) + stop(paste0("Error ", version_request$status_code)) } - } diff --git a/R/get_labresults.R b/R/get_labresults.R index fa5af62..9ed02fa 100644 --- a/R/get_labresults.R +++ b/R/get_labresults.R @@ -22,17 +22,27 @@ #' be the default if you are using Go.Data version 2.38.1 #' or newer. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login -#' @param outbreak_id The id number for the outbreak for which you want to download data. -#' @param method The method to download data. `method="export"` is the preferred and default method for Go.Data version 2.38.1 or later. See Details. -#' @param batch_size If `method="batches"`, then `batch_size` specifies the number of records to retrieve in each iteration. -#' @param wait If `method="export"`, then `wait` is the number of seconds to wait in between iterations of checking the status of the export. -#' @param file.type If `method="export"`, then `file.type` determines Whether the resulting data frame should contain nested fields (`file.type="json"`, the default) or an entirely flat data structure (`file.type="csv"`) +#' @param outbreak_id The id number for the outbreak for which you want to +#' download data. +#' @param method The method to download data. `method="export"` is the +#' preferred and default method for Go.Data version 2.38.1 or later. See +#' Details. +#' @param batch_size If `method = "batches"`, then `batch_size` specifies the +#' number of records to retrieve in each iteration. +#' @param wait If `method = "export"`, then `wait` is the number of seconds to +#' wait in between iterations of checking the status of the export. +#' @param file_type If `method = "export"`, then `file_type` determines Whether +#' the resulting data frame should contain nested fields (`file_type = "json"`, +#' the default) or an entirely flat data structure (`file_type = "csv"`) #' #' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' \code{\link[tidyr]{nest}} for assitance with unnesting. #' @export #' #' @examples @@ -42,37 +52,40 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' labresults <- get_labresults(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' labresults <- get_labresults( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck - - -get_labresults <- function(url=url, - username=username, - password=password, - outbreak_id=outbreak_id, - method=c("export","batch"), - batch_size=50000, - wait=2, - file.type=c("json","csv")) { +get_labresults <- function(url, + username, + password, + outbreak_id, + method = c("export", "batch"), + batch_size = 50000, + wait = 2, + file_type = c("json", "csv")) { #Check that outbreak_id is active - if (outbreak_id != get_active_outbreak(url=url, username=username, password=password)) { - set_active_outbreak(url=url, username=username, password=password, outbreak_id=outbreak_id) + active_outbreak <- get_active_outbreak( + url = url, + username = username, + password = password + ) + if (outbreak_id != active_outbreak) { + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) } #Set default method based on current version of Go.Data - version.check <- check_godata_version(url=url) - if (!version.check) { + version_check <- check_godata_version(url = url) + if (!version_check) { method <- "batches" #Older version of Go.Data can only use the batch method } else if (missing(method)) { method <- "export" # For new versions of Go.Data, default to export method @@ -81,31 +94,47 @@ get_labresults <- function(url=url, if (method == "batches") { - api_call_n <- paste0(url, "api/outbreaks/",outbreak_id,"/lab-results/aggregate-filtered-count") - api_call_get <- paste0(url, "api/outbreaks/",outbreak_id,"/lab-results/aggregate") - df <- batch_downloader(url=url, - username=username, - password=password, - api_call_n=api_call_n, - api_call_get=api_call_get, - batch_size=batch_size) + api_call_n <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/lab-results/aggregate-filtered-count" + ) + api_call_get <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/lab-results/aggregate" + ) + df <- batch_downloader( + url = url, + username = username, + password = password, + api_call_n = api_call_n, + api_call_get = api_call_get, + batch_size = batch_size + ) } else if (method == "export") { - #Default value of file.type is "json" - if (missing(file.type)) file.type <- "json" + #Default value of file_type is "json" + if (missing(file_type)) file_type <- "json" #Submit an export request to the system - api_call_request <- paste0(url,"api/outbreaks/",outbreak_id,"/lab-results/export") - df <- export_downloader(url=url, - username=username, - password=password, - api_call_request=api_call_request, - file.type = file.type, - wait = wait) - - + api_call_request <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/lab-results/export" + ) + df <- export_downloader( + url = url, + username = username, + password = password, + api_call_request = api_call_request, + file_type = file_type, + wait = wait + ) } - - return(df) + return(tibble::as_tibble(df)) } diff --git a/R/get_language_tokens.R b/R/get_language_tokens.R index 28c365e..2f0a5af 100644 --- a/R/get_language_tokens.R +++ b/R/get_language_tokens.R @@ -1,13 +1,17 @@ -#' Get a list of variable tokens and their labels for the language you specify, in order to re-code variables in R. +#' Get a list of variable tokens and their labels for the language you specify, +#' in order to re-code variables in R. #' #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login -#' @param language The language ID you are retrieving translation file for, for instance "english_us" +#' @param language The language ID you are retrieving translation file for, +#' for instance "english_us" #' #' @return -#' Returns data frame of language tokens for your language. You will only be able to execute this function if you have access to the language tokens. +#' Returns data frame of language tokens for your language. You will only be +#' able to execute this function if you have access to the language tokens. #' @export #' @examples #' \dontrun{ @@ -16,30 +20,38 @@ #' password <- "mypassword" #' language <- "english_us" #' -#' language_tokens <- get_language_tokens(url=url, -#' username=username, -#' password=password, -#' language=language) +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = language +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck +get_language_tokens <- function(url, + username, + password, + language) { + df_request <- httr::GET( + paste0( + url, + "api/languages/", + language, + "/language-tokens", + "?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) -get_language_tokens <- function(url=url, - username=username, - password=password, - language=language) { + df_content <- httr::content(df_request, as = "text") - df <- GET(paste0(url,"api/languages/",language,"/language-tokens", - "?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - as_tibble() + df <- jsonlite::fromJSON(df_content, flatten = TRUE) - return(df) + df <- tibble::as_tibble(df) + return(df) } diff --git a/R/get_languages.R b/R/get_languages.R index 6777c3e..c227830 100644 --- a/R/get_languages.R +++ b/R/get_languages.R @@ -1,7 +1,8 @@ #' Get lanuages in Go.Data #' #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' @@ -15,28 +16,34 @@ #' password <- "mypassword" #' language <- "english_us" #' -#' languages <- get_languages(url=url, -#' username=username, -#' password=password) +#' languages <- get_languages( +#' url = url, +#' username = username, +#' password = password +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck +get_languages <- function(url, + username, + password) { + df_request <- httr::GET( + paste0( + url, + "api/languages/", + "?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) -get_languages <- function(url=url, - username=username, - password=password) { + df_content <- httr::content(df_request, as = "text") - df <- GET(paste0(url,"api/languages/", - "?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - as_tibble() + df <- jsonlite::fromJSON(df_content, flatten = TRUE) - return(df) + df <- tibble::as_tibble(df) + return(df) } diff --git a/R/get_locations.R b/R/get_locations.R index 1489ba7..decf897 100644 --- a/R/get_locations.R +++ b/R/get_locations.R @@ -6,7 +6,8 @@ #' endpoint). This function relies on the `\locations` #' API endpoint. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' @@ -19,29 +20,33 @@ #' username <- "myemail@email.com" #' password <- "mypassword" #' -#' locations <- get_locations(url=url, -#' username=username, -#' password=password) +#' locations <- get_locations( +#' url = url, +#' username = username, +#' password = password +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck #' @export +get_locations <- function(url, + username, + password) { - -get_locations <- function(url=url, - username=username, - password=password) { - - locations <- GET(paste0(url,"api/locations", - "?access_token=",godataR::get_access_token(url=url, username=username, password=password))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - filter(deleted!=TRUE) + locations_request <- httr::GET( + paste0( + url, + "api/locations", + "?access_token=", + godataR::get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + locations_content <- httr::content(locations_request, as = "text") + locations <- jsonlite::fromJSON(locations_content, flatten = TRUE) + locations <- dplyr::filter(locations, .data$deleted != TRUE) + locations <- tibble::as_tibble(locations) return(locations) - } diff --git a/R/get_reference_data.R b/R/get_reference_data.R index 5e2f0b7..4f78ee6 100644 --- a/R/get_reference_data.R +++ b/R/get_reference_data.R @@ -6,7 +6,8 @@ #' endpoint). This function relies on the #' `\reference-data` API endpoint. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' @@ -19,29 +20,33 @@ #' username <- "myemail@email.com" #' password <- "mypassword" #' -#' reference_data <- get_reference_data(url=url, -#' username=username, -#' password=password) +#' reference_data <- get_reference_data( +#' url = url, +#' username = username, +#' password = password +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck #' @export +get_reference_data <- function(url, + username, + password) { + reference_data_request <- httr::GET( + paste0( + url, + "api/reference-data", + "?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + reference_data_content <- httr::content(reference_data_request, as = "text") + reference_data <- jsonlite::fromJSON(reference_data_content, flatten = TRUE) + reference_data <- dplyr::filter(reference_data, .data$deleted != TRUE) + reference_data <- tibble::as_tibble(reference_data) -get_reference_data <- function(url=url, - username=username, - password=password) { - - reference_data <- GET(paste0(url,"api/reference-data", - "?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - filter(deleted!=TRUE) - - return(reference_data) - + return(reference_data) } diff --git a/R/get_relationships.R b/R/get_relationships.R index cef852a..5c859fc 100644 --- a/R/get_relationships.R +++ b/R/get_relationships.R @@ -6,33 +6,43 @@ #' This function works on all versions of Go.Data. There #' are two methods for downloading the data: #' -#' `method="batches"` will work on all versions of +#' `method = "batches"` will work on all versions of #' Go.Data. This method relies on the GET outbreak/{id}/relationships #' API endpoint. Records are then retrieved in batches #' based on `batch_size` and appended together into -#' a final dataset. `method="batches"` will be the default and +#' a final dataset. `method = "batches"` will be the default and #' only available method for Go.Data version 2.38.0 or older. #' -#' `method="export"` will only work on Go.Data versions +#' `method = "export"` will only work on Go.Data versions #' 2.38.1 or newer. This method relies on the GET #' outbreak/{id}/relationships/export API endpoint. An export #' request is submitted to the server, and then when the #' export is ready, it will be downloaded. Due to better -#' performance and more options, `method="export"` will +#' performance and more options, `method = "export"` will #' be the default if you are using Go.Data version 2.38.1 #' or newer. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login -#' @param outbreak_id The id number for the outbreak for which you want to download data. -#' @param method The method to download data. `method="export"` is the preferred and default method for Go.Data version 2.38.1 or later. See Details. -#' @param batch_size If `method="batches"`, then `batch_size` specifies the number of records to retrieve in each iteration. -#' @param wait If `method="export"`, then `wait` is the number of seconds to wait in between iterations of checking the status of the export. -#' @param file.type If `method="export"`, then `file.type` determines Whether the resulting data frame should contain nested fields (`file.type="json"`, the default) or an entirely flat data structure (`file.type="csv"`) +#' @param outbreak_id The id number for the outbreak for which you want to +#' download data. +#' @param method The method to download data. `method = "export"` is the +#' preferred and default method for Go.Data version 2.38.1 or later. See +#' Details. +#' @param batch_size If `method = "batches"`, then `batch_size` specifies the +#' number of records to retrieve in each iteration. +#' @param wait If `method = "export"`, then `wait` is the number of seconds to +#' wait in between iterations of checking the status of the export. +#' @param file_type If `method = "export"`, then `file_type` determines Whether +#' the resulting data frame should contain nested fields (`file_type = "json"`, +#' the default) or an entirely flat data structure (`file_type = "csv"`) #' #' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' \code{\link[tidyr]{nest}} for assitance with unnesting. #' @export #' #' @examples @@ -42,37 +52,40 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' relationships <- get_relationships(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' relationships <- get_relationships( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @import tibble -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck - - -get_relationships <- function(url=url, - username=username, - password=password, - outbreak_id=outbreak_id, - method=c("export","batch"), - batch_size=50000, - wait=2, - file.type=c("json","csv")) { +get_relationships <- function(url, + username, + password, + outbreak_id, + method = c("export", "batch"), + batch_size = 50000, + wait = 2, + file_type = c("json", "csv")) { #Check that outbreak_id is active - if (outbreak_id != get_active_outbreak(url=url, username=username, password=password)) { - set_active_outbreak(url=url, username=username, password=password, outbreak_id=outbreak_id) + active_outbreak <- get_active_outbreak( + url = url, + username = username, + password = password + ) + if (outbreak_id != active_outbreak) { + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) } #Set default method based on current version of Go.Data - version.check <- check_godata_version(url=url) - if (!version.check) { + version_check <- check_godata_version(url = url) + if (!version_check) { method <- "batches" #Older version of Go.Data can only use the batch method } else if (missing(method)) { method <- "export" # For new versions of Go.Data, default to export method @@ -81,31 +94,47 @@ get_relationships <- function(url=url, if (method == "batches") { - api_call_n <- paste0(url, "api/outbreaks/",outbreak_id,"/relationships/count") - api_call_get <- paste0(url, "api/outbreaks/",outbreak_id,"/relationships") - df <- batch_downloader(url=url, - username=username, - password=password, - api_call_n=api_call_n, - api_call_get=api_call_get, - batch_size=batch_size) + api_call_n <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/relationships/count" + ) + api_call_get <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/relationships" + ) + df <- batch_downloader( + url = url, + username = username, + password = password, + api_call_n = api_call_n, + api_call_get = api_call_get, + batch_size = batch_size + ) } else if (method == "export") { - #Default value of file.type is "json" - if (missing(file.type)) file.type <- "json" + #Default value of file_type is "json" + if (missing(file_type)) file_type <- "json" #Submit an export request to the system - api_call_request <- paste0(url,"api/outbreaks/",outbreak_id,"/relationships/export") - df <- export_downloader(url=url, - username=username, - password=password, - api_call_request=api_call_request, - file.type = file.type, - wait = wait) - - + api_call_request <- paste0( + url, + "api/outbreaks/", + outbreak_id, + "/relationships/export" + ) + df <- export_downloader( + url = url, + username = username, + password = password, + api_call_request = api_call_request, + file_type = file_type, + wait = wait + ) } - - return(df) + return(tibble::as_tibble(df)) } diff --git a/R/get_teams.R b/R/get_teams.R index d6b799e..72ffd34 100644 --- a/R/get_teams.R +++ b/R/get_teams.R @@ -6,7 +6,8 @@ #' endpoint). This function relies on the #' `\teams` API endpoint. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' @@ -19,29 +20,33 @@ #' username <- "myemail@email.com" #' password <- "mypassword" #' -#' teams <- get_teams(url=url, -#' username=username, -#' password=password) +#' teams <- get_teams( +#' url = url, +#' username = username, +#' password = password +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck #' @export +get_teams <- function(url, + username, + password) { - -get_teams <- function(url=url, - username=username, - password=password) { - - teams <- GET(paste0(url,"api/teams", - "?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - filter(deleted!=TRUE) + teams_request <- httr::GET( + paste0( + url, + "api/teams", + "?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + teams_content <- httr::content(teams_request, as = "text") + teams <- jsonlite::fromJSON(teams_content, flatten = TRUE) + teams <- dplyr::filter(teams, .data$deleted != TRUE) + teams <- tibble::as_tibble(teams) return(teams) - } diff --git a/R/get_users.R b/R/get_users.R index b318be1..8f1765b 100644 --- a/R/get_users.R +++ b/R/get_users.R @@ -6,12 +6,15 @@ #' endpoint). This function relies on the #' `\users` API endpoint. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' #' @return -#' Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +#' Returns a data frame. Some fields, such as addresses, hospitalization +#' history, and questionnaire fields may require further unnesting. See +#' \code{\link[tidyr]{nest}} for assitance with unnesting. #' @export #' @examples #' \dontrun{ @@ -19,30 +22,33 @@ #' username <- "myemail@email.com" #' password <- "mypassword" #' -#' users <- get_users(url=url, -#' username=username, -#' password=password) +#' users <- get_users( +#' url = url, +#' username = username, +#' password = password +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON -#' @importFrom purrr pluck #' @export +get_users <- function(url, + username, + password) { + users_request <- httr::GET( + paste0( + url, + "api/users", + "?access_token=", + godataR::get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + users_content <- httr::content(users_request, as = "text") + users <- jsonlite::fromJSON(users_content, flatten = TRUE) + users <- dplyr::filter(users, .data$deleted != TRUE) + users <- tibble::as_tibble(users) - -get_users <- function(url=url, - username=username, - password=password) { - - users <- GET(paste0(url,"api/users", - "?access_token=",godataR::get_access_token(url=url, username=username, password=password))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - filter(deleted!=TRUE) %>% - #select(id, firstName, lastName, email, roleIds, lastLoginDate, institutionName, disregardGeographicRestrictions, activeOutbreakId, createdBy, createdAt) return(users) - } diff --git a/R/mongify_date.R b/R/mongify_date.R index 7006a9d..90e07cf 100644 --- a/R/mongify_date.R +++ b/R/mongify_date.R @@ -33,7 +33,8 @@ #' must be in the same format). #' #' Dates with and without a time-stamp are both accepted. Dates which do include -#' a time stamp must include hours, minutes and seconds in the format **HH:mm:ss** +#' a time stamp must include hours, minutes and seconds in the format +#' **HH:mm:ss** #' #' @md #' @@ -57,32 +58,36 @@ #' #' @export mongify_date <- function(dates, - dateformat = "undefined"){ + dateformat = "undefined") { # Define format of input dates: - if(dateformat == "undefined"){ - format2search = c("ymd_HMS", "dmy_HMS", "mdy_HMS")} - if(dateformat == "ymd"){format2search = "ymd_HMS"} - if(dateformat == "dmy"){format2search = "dmy_HMS"} - if(dateformat == "mdy"){format2search = "mdy_HMS"} + if (dateformat == "undefined") { + format2search <- c("ymd_HMS", "dmy_HMS", "mdy_HMS") + } + if (dateformat == "ymd") format2search <- "ymd_HMS" + if (dateformat == "dmy") format2search <- "dmy_HMS" + if (dateformat == "mdy") format2search <- "mdy_HMS" # Make sure dates are strings: - dates = as.character(dates) + dates <- as.character(dates) # Check if dates already have a time-stamp, if not add time: - dflong = ifelse(nchar(dates) %in% c(8, 10), - paste0(dates, " 00:00:00"), - dates) + dflong <- ifelse( + nchar(dates) %in% c(8, 10), + paste0(dates, " 00:00:00"), + dates + ) # Convert date-times to posixct format: - dfpct = lubridate::parse_date_time(x = dflong, - orders = format2search, - tz = "UTC") + dfpct <- lubridate::parse_date_time( + x = dflong, + orders = format2search, + tz = "UTC" + ) # Add the Godata / Mongodb specific format ending for date-time: - dfmongo = format(x = dfpct, format = "%Y-%m-%dT%H:%M:%S.000Z") + dfmongo <- format(x = dfpct, format = "%Y-%m-%dT%H:%M:%S.000Z") # Return the formatted dates: return(dfmongo) - } diff --git a/R/set_active_outbreak.R b/R/set_active_outbreak.R index 530dcd7..1b112d2 100644 --- a/R/set_active_outbreak.R +++ b/R/set_active_outbreak.R @@ -8,7 +8,8 @@ #' Each Go.Data user can have 1 and only 1 active #' outbreak at a given time. #' -#' @param url Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end! +#' @param url Insert the base URL for your instance of Go.Data here. Don't +#' forget the forward slash "/" at end! #' @param username The email address for your Go.Data login. #' @param password The password for your Go.Data login #' @param outbreak_id The id number for the outbreak to set to active. @@ -23,53 +24,92 @@ #' password <- "mypassword" #' outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" #' -#' set_active_outbreak(url=url, -#' username=username, -#' password=password, -#' outbreak_id=outbreak_id) +#' set_active_outbreak( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) #' } -#' @importFrom magrittr %>% -#' @import dplyr -#' @import tidyr -#' @import httr -#' @importFrom jsonlite fromJSON +set_active_outbreak <- function(url, + username, + password, + outbreak_id) { -set_active_outbreak <- function(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) { + #Get User ID & Active Outbreak ID + user_details_request <- httr::GET( + paste0( + url, + "api/users", + "?access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + user_details_content <- httr::content(user_details_request, as = "text") - #Get User ID & Active Outbreak ID - user.details <- GET(paste0(url,"api/users", - "?access_token=",get_access_token(url=url, username=username, password=password))) %>% - content(as="text") %>% - fromJSON(flatten=TRUE) %>% - filter(email==username) + user_details <- jsonlite::fromJSON(user_details_content, flatten = TRUE) + + user_details <- dplyr::filter(user_details, .data$email == username) - current.active.outbreak <- user.details$activeOutbreakId - user.id <- user.details$id + current_active_outbreak <- user_details$activeOutbreakId + user_id <- user_details$id #Get List of Available Outbreak IDs - available.outbreaks <- get_all_outbreaks(url=url, username=username, password=password) %>% - select(id) %>% unlist() + available_outbreaks <- get_all_outbreaks( + url = url, + username = username, + password = password + ) - if (current.active.outbreak == outbreak_id) { #Is outbreak_id already active? - text <- paste0("Active outbreak not changed. ", outbreak_id, " is already active.") - } else if (!(outbreak_id %in% available.outbreaks)) { - stop(paste0("Active outbreak not changed. ",outbreak_id, " not in list of user's available outbreaks. Make sure the id number is correct & that the user has proper access.")) + available_outbreaks <- unlist(dplyr::select(available_outbreaks, id)) + + + if (current_active_outbreak == outbreak_id) { # Is outbreak_id already active? + text <- paste0( + "Active outbreak not changed. ", + outbreak_id, + " is already active." + ) + } else if (!(outbreak_id %in% available_outbreaks)) { + stop(paste0( + "Active outbreak not changed. ", + outbreak_id, + " not in list of user's available outbreaks. Make sure the id number is", + " correct & that the user has proper access." + )) } else { - new.data <- list("activeOutbreakId"=outbreak_id) - patch.active.outbreak <- PATCH(paste0(url,"api/users/",user.id), - add_headers(Authorization = paste("Bearer", get_access_token(url=url, username=username, password=password), sep = " ")), - body=new.data, - encode="json") + new_data <- list("activeOutbreakId" = outbreak_id) + patch_active_outbreak <- httr::PATCH( + paste0( + url, + "api/users/", + user_id + ), + httr::add_headers( + Authorization = paste( + "Bearer", + get_access_token( + url = url, + username = username, + password = password + ), + sep = " " + ) + ), + body = new_data, + encode = "json" + ) text <- paste0("Active outbreak changed! ", outbreak_id, " is now active.") } message(text) - + invisible(outbreak_id) } diff --git a/R/translate_categories.R b/R/translate_categories.R new file mode 100644 index 0000000..a10a81d --- /dev/null +++ b/R/translate_categories.R @@ -0,0 +1,95 @@ +#' Translates cateogories with API labels to more readable forms using the +#' translation specified in the output of `get_language_tokens()`. +#' +#' @param data A data frame (or data frame extension) +#' @param language_tokens A data frame (or data frame extension) containing +#' the translations. Output from `get_language_tokens()` +#' +#' @return A tibble +#' @export +#' +#' @examples +#' \dontrun{ +#' cases <- get_cases( +#' url = url, +#' username = username, +#' password = password, +#' outbreak_id = outbreak_id +#' ) +#' +#' language_tokens <- get_language_tokens( +#' url = url, +#' username = username, +#' password = password, +#' language = "english_us" +#' ) +#' +#' translate_categories( +#' data = cases, +#' language_tokens = language_tokens +#' ) +#' } +translate_categories <- function(data, language_tokens) { + + stopifnot( + "data must be tabular data" = + is.data.frame(data), + "language_tokens must be tabular data" = + is.data.frame(language_tokens) + ) + + if (isFALSE(any_tokens(data, language_tokens))) { + return(data) + } + + data <- translate_token(data = data, language_tokens = language_tokens) + + data <- tibble::as_tibble(data) + + return(data) +} + +#' Translates tokens given a specified translation. +#' +#' @description This function does all the translation for +#' `translate_categories`. +#' +#' @inheritParams translate_categories +#' +#' @return A tibble +#' @keywords internal +translate_token <- function(data, language_tokens) { + if (is.list(data)) { + for (i in seq_along(data)) { + if (any_tokens(data[[i]], language_tokens)) { + data[[i]] <- translate_token( + data = data[[i]], + language_tokens = language_tokens + ) + } + } + } else { + token_index <- lapply( + data, + function(x) which(language_tokens$tokens$token %in% x) + ) + token_index <- unlist( + lapply( + token_index, + function(x) if (length(x) == 0) NA else x + ) + ) + data <- language_tokens$tokens$translation[token_index] + } + return(data) +} + +#' Checks if there are any recognized tokens in the data provided +#' +#' @inheritParams translate_categories +#' +#' @return Boolean logical (TRUE or FALSE) +#' @keywords internal +any_tokens <- function(data, language_tokens) { + any(unname(unlist(data)) %in% language_tokens$tokens$token) +} diff --git a/man/any_tokens.Rd b/man/any_tokens.Rd new file mode 100644 index 0000000..31a91ef --- /dev/null +++ b/man/any_tokens.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/translate_categories.R +\name{any_tokens} +\alias{any_tokens} +\title{Checks if there are any recognized tokens in the data provided} +\usage{ +any_tokens(data, language_tokens) +} +\arguments{ +\item{data}{A data frame (or data frame extension)} + +\item{language_tokens}{A data frame (or data frame extension) containing +the translations. Output from \code{get_language_tokens()}} +} +\value{ +Boolean logical (TRUE or FALSE) +} +\description{ +Checks if there are any recognized tokens in the data provided +} +\keyword{internal} diff --git a/man/batch_downloader.Rd b/man/batch_downloader.Rd index e70e3de..c1f83c7 100644 --- a/man/batch_downloader.Rd +++ b/man/batch_downloader.Rd @@ -4,17 +4,11 @@ \alias{batch_downloader} \title{Function to manage batch downloads} \usage{ -batch_downloader( - url = url, - username = username, - password = password, - api_call_n = api_call_n, - api_call_get = api_call_get, - batch_size = batch_size -) +batch_downloader(url, username, password, api_call_n, api_call_get, batch_size) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} @@ -24,10 +18,13 @@ batch_downloader( \item{api_call_get}{The API url to GET the records.} -\item{batch_size}{Specifies the number of records to retrieve in each iteration.} +\item{batch_size}{Specifies the number of records to retrieve in each +iteration.} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\verb{\link[tidyr]\{nest\}} for assitance with unnesting. } \description{ A housekeeping function to do batch downloads. @@ -39,9 +36,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -cases <- get_cases(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/cases_from_contacts.Rd b/man/cases_from_contacts.Rd new file mode 100644 index 0000000..e9a91d6 --- /dev/null +++ b/man/cases_from_contacts.Rd @@ -0,0 +1,47 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/cases_from_contacts.R +\name{cases_from_contacts} +\alias{cases_from_contacts} +\title{Pull out all cases that used to be contacts} +\usage{ +cases_from_contacts(cases_clean) +} +\arguments{ +\item{cases_clean}{The cleaned case data. Case data is returned by +\code{\link[=get_cases]{get_cases()}} and cleaned by \code{\link[=clean_cases]{clean_cases()}}.} +} +\value{ +A tibble containing the cases that used to be contacts. +} +\description{ +Pull out all cases that used to be contacts +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +# other cleaned data required for `clean_cases()` +cases_vacc_history_clean <- clean_case_vax_history(cases = cases) +cases_address_history_clean <- clean_case_address_history(cases = cases) +cases_dateranges_history_clean <- clean_case_med_history(cases = cases) + +cases_clean <- clean_cases( + cases = cases, + cases_address_history_clean = cases_address_history_clean, + cases_vacc_history_clean = cases_vacc_history_clean, + cases_dateranges_history_clean = cases_dateranges_history_clean +) + +cases_from_contacts <- cases_from_contacts(cases_clean = cases_clean) +} +} diff --git a/man/check_godata_url.Rd b/man/check_godata_url.Rd index 08c91e2..ad76c37 100644 --- a/man/check_godata_url.Rd +++ b/man/check_godata_url.Rd @@ -4,10 +4,14 @@ \alias{check_godata_url} \title{Check if the provided Go.Data URL is valid} \usage{ -check_godata_url(url = url) +check_godata_url(url, success_code = 200) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} + +\item{success_code}{A numeric specifying which code is returned by the API +when successfully returning the status code. Default is 200.} } \value{ Boolean, where \code{TRUE} indicates a valid URL. @@ -20,6 +24,6 @@ used in many of the other \code{godataR} functions. \examples{ \dontrun{ url <- "https://MyGoDataServer.com/" -check_godata_url(url=url) +check_godata_url(url = url) } } diff --git a/man/check_godata_version.Rd b/man/check_godata_version.Rd index 42bbb5c..46c6dff 100644 --- a/man/check_godata_version.Rd +++ b/man/check_godata_version.Rd @@ -7,7 +7,8 @@ check_godata_version(url = url) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} } \value{ Boolean, where \code{TRUE} indicates version 2.38.1 or later. @@ -21,6 +22,6 @@ many of the other \code{godataR} functions. \examples{ \dontrun{ url <- "https://MyGoDataServer.com/" -check_godata_version(url=url) +check_godata_version(url = url) } } diff --git a/man/clean_case_address_history.Rd b/man/clean_case_address_history.Rd new file mode 100644 index 0000000..cab3aea --- /dev/null +++ b/man/clean_case_address_history.Rd @@ -0,0 +1,62 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_case_address_history.R +\name{clean_case_address_history} +\alias{clean_case_address_history} +\title{Extract address information from case data} +\usage{ +clean_case_address_history(cases, locations_clean, language_tokens) +} +\arguments{ +\item{cases}{A tibble with case data. Case data is returned by +\code{\link[=get_cases]{get_cases()}}.} + +\item{locations_clean}{A tibble with cleaned locations data. Locations data +is returned by \code{\link[=get_locations]{get_locations()}} and cleaned by \code{\link[=clean_locations]{clean_locations()}}.} + +\item{language_tokens}{A tibble of language tokens returned by +\code{\link[=get_language_tokens]{get_language_tokens()}} to translate the string tokens in the data.} +} +\value{ +A tibble with address information from cases data. +} +\description{ +This function un-nests and cleans the address data and stores +it in a standalone table with all addresses, even if there is more than 1 +per person. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +locations <- get_locations( + url = url, + username = username, + password = password +) + +locations_clean <- clean_locations(locations = locations) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +case_address_history <- clean_case_address_history( + cases = cases, + locations_clean = locations_clean, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_case_med_history.Rd b/man/clean_case_med_history.Rd new file mode 100644 index 0000000..120ce53 --- /dev/null +++ b/man/clean_case_med_history.Rd @@ -0,0 +1,49 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_case_med_history.R +\name{clean_case_med_history} +\alias{clean_case_med_history} +\title{Extracts and cleans medical history from case data} +\usage{ +clean_case_med_history(cases, language_tokens) +} +\arguments{ +\item{cases}{A tibble with case data. Case data is returned by +\code{\link[=get_cases]{get_cases()}}.} + +\item{language_tokens}{A tibble of language tokens returned by +\code{\link[=get_language_tokens]{get_language_tokens()}} to translate the string tokens in the data.} +} +\value{ +A tibble with information on isolation and hospitalization history. +} +\description{ +This function un-nests and cleans date ranges of isolation and +hospitalization history and stores it in a standalone table. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +cases_med_history <- clean_case_med_history( + cases = cases, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_case_vax_history.Rd b/man/clean_case_vax_history.Rd new file mode 100644 index 0000000..5b3121e --- /dev/null +++ b/man/clean_case_vax_history.Rd @@ -0,0 +1,48 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_case_vax_history.R +\name{clean_case_vax_history} +\alias{clean_case_vax_history} +\title{Cleans vaccination data from case data} +\usage{ +clean_case_vax_history(cases, language_tokens) +} +\arguments{ +\item{cases}{A tibble with address information from cases data.} + +\item{language_tokens}{A tibble of language tokens returned by +\code{\link[=get_language_tokens]{get_language_tokens()}} to translate the string tokens in the data.} +} +\value{ +A tibble with cleaned and un-nested vaccination history data. +} +\description{ +Cleans and un-nests vaccination history, where vaccination is +complete, from case data. Case data is returned from \code{\link[=get_cases]{get_cases()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +vax_history <- clean_case_vax_history( + cases = cases, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_cases.Rd b/man/clean_cases.Rd new file mode 100644 index 0000000..4e86764 --- /dev/null +++ b/man/clean_cases.Rd @@ -0,0 +1,82 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_cases.R +\name{clean_cases} +\alias{clean_cases} +\title{Cleans case data} +\usage{ +clean_cases( + cases, + cases_address_history_clean, + cases_vacc_history_clean, + cases_dateranges_history_clean, + language_tokens +) +} +\arguments{ +\item{cases}{A \code{tibble} containing the case data.} + +\item{language_tokens}{A tibble of language tokens returned by +\code{\link[=get_language_tokens]{get_language_tokens()}} to translate the string tokens in the data.} + +\item{locations_clean}{A tibble with cleaned locations data. Locations data +is returned by \code{\link[=get_locations]{get_locations()}} and cleaned by \code{\link[=clean_locations]{clean_locations()}}.} +} +\value{ +A \code{tibble} containing the cleaned case data. +} +\description{ +Cleans and un-nests case data. Case data is returned by +\code{\link[=get_cases]{get_cases()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +locations <- get_locations( + url = url, + username = username, + password = password +) +locations_clean <- clean_locations(locations = locations) + +# other cleaned data required for `clean_cases()` +cases_vacc_history_clean <- clean_case_vax_history( + cases = cases, + language_tokens = language_tokens +) +cases_address_history_clean <- clean_case_address_history( + cases = cases, + locations_clean = locations_clean, + language_tokens = language_tokens +) +cases_dateranges_history_clean <- clean_case_med_history( + cases = cases, + language_tokens = language_tokens +) + +cases_clean <- clean_cases( + cases = cases, + cases_address_history_clean = cases_address_history_clean, + cases_vacc_history_clean = cases_vacc_history_clean, + cases_dateranges_history_clean = cases_dateranges_history_clean, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_contact_address_history.Rd b/man/clean_contact_address_history.Rd new file mode 100644 index 0000000..16b3c8e --- /dev/null +++ b/man/clean_contact_address_history.Rd @@ -0,0 +1,61 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_contact_address_history.R +\name{clean_contact_address_history} +\alias{clean_contact_address_history} +\title{Extracts address information from contact data} +\usage{ +clean_contact_address_history(contacts, locations_clean, language_tokens) +} +\arguments{ +\item{contacts}{A tibble with contacts data. Contacts data is returned by +\code{\link[=get_contacts]{get_contacts()}}.} + +\item{locations_clean}{A tibble with cleaned locations data. Locations data +is returned by \code{\link[=get_locations]{get_locations()}} and cleaned by \code{\link[=clean_locations]{clean_locations()}}.} + +\item{language_tokens}{A tibble of language tokens returned by +\code{\link[=get_language_tokens]{get_language_tokens()}} to translate the string tokens in the data.} +} +\value{ +A tibble with address information from contacts data. +} +\description{ +This function un-nests and cleans the address data and stores +it in a standalone table with all addresses, even if there is more than 1 +per person. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +contacts <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +locations <- get_locations( + url = url, + username = username, + password = password +) +locations_clean <- clean_locations(locations = locations) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +contact_address_history <- clean_contact_address_history( + contacts = contacts, + locations_clean = locations_clean, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_contact_vax_history.Rd b/man/clean_contact_vax_history.Rd new file mode 100644 index 0000000..c7e49f3 --- /dev/null +++ b/man/clean_contact_vax_history.Rd @@ -0,0 +1,49 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_contact_vax_history.R +\name{clean_contact_vax_history} +\alias{clean_contact_vax_history} +\title{Cleans vaccination data from contact data} +\usage{ +clean_contact_vax_history(contacts, language_tokens) +} +\arguments{ +\item{contacts}{A tibble with address information from contact data.} + +\item{language_tokens}{A tibble of language tokens returned by +\code{\link[=get_language_tokens]{get_language_tokens()}} to translate the string tokens in the data.} +} +\value{ +A tibble with cleaned and un-nested vaccination history data. +} +\description{ +Cleans and un-nests vaccination history, where vaccination is +complete, from contact data. Contact data is returned from +\code{\link[=get_contacts]{get_contacts()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +contacts <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +vax_history <- clean_contact_vax_history( + contacts = contacts, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_contacts.Rd b/man/clean_contacts.Rd new file mode 100644 index 0000000..a5d5f90 --- /dev/null +++ b/man/clean_contacts.Rd @@ -0,0 +1,112 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_contacts.R +\name{clean_contacts} +\alias{clean_contacts} +\title{Clean contacts data} +\usage{ +clean_contacts( + contacts, + contacts_address_history_clean, + contacts_vacc_history_clean, + contacts_becoming_cases +) +} +\arguments{ +\item{contacts}{A \code{tibble} containing the contact data.} + +\item{contacts_address_history_clean}{A \code{tibble} containing the cleaned +address history data from contacts (data is cleaned by +\code{\link[=clean_contact_address_history]{clean_contact_address_history()}}.} + +\item{contacts_vacc_history_clean}{A \code{tibble} containing the cleaned +vaccination history data from contacts (data is cleaned by +\code{\link[=clean_contact_vax_history]{clean_contact_vax_history()}}.} + +\item{contacts_becoming_cases}{A \code{tibble} containing the cleaned data on +contacts that became cases (date is produced using +\code{\link[=cases_from_contacts]{cases_from_contacts()}}).} +} +\value{ +A \code{tibble} containing the cleaned case data. +} +\description{ +Cleans and un-nests contact data. Contact data is returned by +\code{\link[=get_contacts]{get_contacts()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +contacts <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +locations <- get_locations( + url = url, + username = username, + password = password +) + +locations_clean <- clean_locations(locations = locations) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +# other cleaned data required for `clean_contacts()` +contacts_vacc_history_clean <- clean_contact_vax_history( + contacts = contacts, + language_tokens = language_tokens +) +contacts_address_history_clean <- clean_contact_address_history( + contacts = contacts, + locations_clean = locations_clean, + language_tokens = language_tokens +) + +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) +cases_address_history_clean <- clean_case_address_history( + cases = cases, + locations_clean = locations_clean, + language_tokens = language_tokens +) +cases_vacc_history_clean <- clean_case_vax_history( + cases = cases, + language_tokens = language_tokens +) +cases_dateranges_history_clean <- clean_case_med_history( + cases = cases, + language_tokens = language_tokens +) + +cases_clean <- clean_cases( + cases = cases, + cases_address_history_clean = cases_address_history_clean, + cases_vacc_history_clean = cases_vacc_history_clean, + cases_dateranges_history_clean = cases_dateranges_history_clean, + language_tokens = language_tokens +) +contacts_becoming_cases <- cases_from_contacts(cases_clean = cases_clean) + +contacts_clean <- clean_contacts( + contacts = contacts, + contacts_address_history_clean = cases_address_history_clean, + contacts_vacc_history_clean = cases_vacc_history_clean, + contacts_becoming_cases = contacts_becoming_cases +) +} +} diff --git a/man/clean_contacts_of_contacts.Rd b/man/clean_contacts_of_contacts.Rd new file mode 100644 index 0000000..1b31a65 --- /dev/null +++ b/man/clean_contacts_of_contacts.Rd @@ -0,0 +1,83 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_contacts_of_contacts.R +\name{clean_contacts_of_contacts} +\alias{clean_contacts_of_contacts} +\title{Clean contacts of contacts data} +\usage{ +clean_contacts_of_contacts( + contacts_of_contacts, + contacts_of_contacts_address_history_clean, + contacts_of_contacts_vacc_history_clean, + language_tokens +) +} +\arguments{ +\item{contacts_of_contacts}{A \code{tibble} containing the contacts of contacts +data.} + +\item{contacts_of_contacts_address_history_clean}{A \code{tibble} containing the +cleaned address history data from contacts of contacts (data is cleaned by +\code{\link[=clean_contacts_of_contacts_address_history]{clean_contacts_of_contacts_address_history()}}).} + +\item{contacts_of_contacts_vacc_history_clean}{A \code{tibble} containing the +cleaned vaccination history from contacts of contacts (data is cleaned by +\code{\link[=clean_contacts_of_contacts_vax_history]{clean_contacts_of_contacts_vax_history()}}).} + +\item{language_tokens}{A tibble of language tokens returned by +\code{\link[=get_language_tokens]{get_language_tokens()}} to translate the string tokens in the data.} +} +\value{ +A \code{tibble} containing the cleaned contacts of contacts data. +} +\description{ +Cleans and un-nests contacts of contacts data. Contacts of +contacts data is returned by \code{\link[=get_contacts_of_contacts]{get_contacts_of_contacts()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +contacts_of_contacts <- get_contacts_of_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +locations <- get_locations( + url = url, + username = username, + password = password +) + +locations_clean <- clean_locations(locations = locations) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +contacts_of_contacts_address_history_clean <- clean_contacts_of_contacts_address_history( + contacts_of_contacts = contacts_of_contacts, + locations_clean = locations_clean, + language_tokens = language_tokens +) + +contacts_of_contacts_vacc_history_clean <- clean_contacts_of_contacts_vax_history( + contacts_of_contacts = contacts_of_contacts, + language_tokens = language_tokens +) + +contacts_of_contacts_clean <- clean_contacts_of_contacts( + contacts_of_contacts = contacts_of_contacts, + contacts_of_contacts_address_history_clean = contacts_of_contacts_address_history_clean, + contacts_of_contacts_vacc_history_clean = contacts_of_contacts_vacc_history_clean, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_contacts_of_contacts_address_history.Rd b/man/clean_contacts_of_contacts_address_history.Rd new file mode 100644 index 0000000..92b1a46 --- /dev/null +++ b/man/clean_contacts_of_contacts_address_history.Rd @@ -0,0 +1,65 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_contacts_of_contacts_address_history.R +\name{clean_contacts_of_contacts_address_history} +\alias{clean_contacts_of_contacts_address_history} +\title{Extracts address information from contacts of contacts data} +\usage{ +clean_contacts_of_contacts_address_history( + contacts_of_contacts, + locations_clean, + language_tokens +) +} +\arguments{ +\item{contacts_of_contacts}{A\code{tibble} with contacts of contacts data. +Contacts of contacts data is returned by \code{\link[=get_contacts_of_contacts]{get_contacts_of_contacts()}}.} + +\item{locations_clean}{A \code{tibble} with cleaned location data. Location data +is returned by \code{\link[=get_locations]{get_locations()}} and cleaned by \code{\link[=clean_locations]{clean_locations()}}.} + +\item{language_tokens}{A tibble of language tokens returned by +\code{\link[=get_language_tokens]{get_language_tokens()}} to translate the string tokens in the data.} +} +\value{ +A \code{tibble} with address information from contacts of contacts data. +} +\description{ +This function un-nests and cleans the address data and stores +it in a standalone table with all addresses, even if there is more than 1 +per person. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +contacts_of_contacts <- get_contacts_of_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +locations <- get_locations( + url = url, + username = username, + password = password +) +locations_clean <- clean_locations(locations = locations) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +contact_of_contacts_add_hist <- clean_contacts_of_contacts_address_history( + contacts_of_contacts = contacts_of_contacts, + locations_clean = locations_clean, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_contacts_of_contacts_vax_history.Rd b/man/clean_contacts_of_contacts_vax_history.Rd new file mode 100644 index 0000000..fd355e1 --- /dev/null +++ b/man/clean_contacts_of_contacts_vax_history.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_contacts_of_contacts_vax_history.R +\name{clean_contacts_of_contacts_vax_history} +\alias{clean_contacts_of_contacts_vax_history} +\title{Cleans vaccination data from contacts of contacts data} +\usage{ +clean_contacts_of_contacts_vax_history(contacts_of_contacts, language_tokens) +} +\arguments{ +\item{contacts_of_contacts}{A \code{tibble} with address information from contacts +of contacts data.} + +\item{language_tokens}{A tibble of language tokens returned by +\code{\link[=get_language_tokens]{get_language_tokens()}} to translate the string tokens in the data.} +} +\value{ +A \code{tibble} with cleaned and un-nested vaccination history data. +} +\description{ +Cleans and un-nests vaccination history, where vaccination is +complete, from contacts of contacts data. Contacts of contacts data is +returned from \code{\link[=get_contacts_of_contacts]{get_contacts_of_contacts()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +contacts_of_contacts <- get_contacts_of_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +vax_history <- clean_contacts_of_contacts_vax_history( + contacts_of_contacts = contacts_of_contacts, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_events.Rd b/man/clean_events.Rd new file mode 100644 index 0000000..2e444d3 --- /dev/null +++ b/man/clean_events.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_events.R +\name{clean_events} +\alias{clean_events} +\title{Clean events data} +\usage{ +clean_events(events, locations_clean) +} +\arguments{ +\item{events}{A \code{tibble} with events data. Events data is returned by +\code{\link[=get_events]{get_events()}}.} + +\item{locations_clean}{A \code{tibble} with cleaned location data. Location data +is returned by \code{\link[=get_locations]{get_locations()}} and cleaned by \code{\link[=clean_locations]{clean_locations()}}. +Make sure the locations data is cleaned prior to supplying it to +\code{clean_events()}.} +} +\value{ +A \code{tibble} with cleaned events data. +} +\description{ +Cleans and un-nests events data which is returned from +\code{\link[=get_events]{get_events()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +events <- get_events( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +locations <- get_locations( + url = url, + username = username, + password = password +) +locations_clean <- clean_locations(locations = locations) + +clean_events <- clean_events( + events = events, + locations_clean = locations_clean) +} +} diff --git a/man/clean_followups.Rd b/man/clean_followups.Rd new file mode 100644 index 0000000..a4d3745 --- /dev/null +++ b/man/clean_followups.Rd @@ -0,0 +1,72 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_followups.R +\name{clean_followups} +\alias{clean_followups} +\title{Clean followup data} +\usage{ +clean_followups(followups, contacts_address_history_clean, language_tokens) +} +\arguments{ +\item{followups}{A \code{tibble} with events data. Followup data is returned by +\code{\link[=get_followups]{get_followups()}}.} + +\item{contacts_address_history_clean}{A \code{tibble} with cleaned address +history data from contacts. Contacts data is returned by \code{\link[=get_contacts]{get_contacts()}} +and cleaned by \code{\link[=clean_contact_address_history]{clean_contact_address_history()}}.} +} +\value{ +A \code{tibble} with cleaned followup data. +} +\description{ +Cleans and un-nests followup data which is returned from +\code{\link[=get_followups]{get_followups()}} +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +followups <- get_followups( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +contacts <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +locations <- get_locations( + url = url, + username = username, + password = password +) + +locations_clean <- clean_locations(locations = locations) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +contacts_address_history_clean <- clean_contact_address_history( + contacts = contacts, + locations_clean = locations_clean, + language_tokens = language_tokens +) + +followups_clean <- clean_followups( + followups = followups, + contacts_address_history_clean = contacts_address_history_clean, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_locations.Rd b/man/clean_locations.Rd new file mode 100644 index 0000000..3752796 --- /dev/null +++ b/man/clean_locations.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_locations.R +\name{clean_locations} +\alias{clean_locations} +\title{Cleans location data} +\usage{ +clean_locations(locations, language_tokens) +} +\arguments{ +\item{locations}{A \code{\link{tibble}} containing locations data. This is the data +returned from \code{\link[=get_locations]{get_locations()}}} +} +\value{ +A \code{tibble} containing the cleaned and rearranged location data. +} +\description{ +Rearrange via joins to get into more usable hierarchy format, +these can then be joined to cases, contacts, etc for further analysis +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" + +locations <- get_locations( + url = url, + username = username, + password = password +) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +clean_locations(locations = locations, language_tokens = language_tokens) +} +} diff --git a/man/clean_relationships.Rd b/man/clean_relationships.Rd new file mode 100644 index 0000000..cedab1a --- /dev/null +++ b/man/clean_relationships.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_relationships.R +\name{clean_relationships} +\alias{clean_relationships} +\title{Cleans relationship data} +\usage{ +clean_relationships(relationships, language_tokens) +} +\arguments{ +\item{relationships}{A \code{tibble} of relationship data. Relationship data is +returned by \code{\link[=get_relationships]{get_relationships()}}.} +} +\value{ +A \code{tibble} with clean relationship data. +} +\description{ +Cleans and un-nests relationship data. Relationship data is +returned by \code{\link[=get_relationships]{get_relationships()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +relationships <- get_relationships( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +clean_relationships <- clean_relationships( + relationships, + language_tokens = language_tokens +) +} +} diff --git a/man/clean_teams.Rd b/man/clean_teams.Rd new file mode 100644 index 0000000..27dfa2e --- /dev/null +++ b/man/clean_teams.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_teams.R +\name{clean_teams} +\alias{clean_teams} +\title{Clean teams data} +\usage{ +clean_teams(teams) +} +\arguments{ +\item{teams}{A \code{tibble} containing teams data. Teams data is returned by +\code{\link[=get_teams]{get_teams()}}.} +} +\value{ +A \code{tibble} of cleaned teams data +} +\description{ +Cleans and un-nests teams data. Teams data is returned by +\code{\link[=get_teams]{get_teams()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" + +teams <- get_teams( + url = url, + username = username, + password = password +) + +clean_teams <- clean_teams(teams) +} +} diff --git a/man/clean_users.Rd b/man/clean_users.Rd new file mode 100644 index 0000000..8c9d11f --- /dev/null +++ b/man/clean_users.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/clean_users.R +\name{clean_users} +\alias{clean_users} +\title{Cleans users data} +\usage{ +clean_users(users, language_tokens) +} +\arguments{ +\item{users}{A \code{tibble} containing users data. Users data is returned by +\code{\link[=get_users]{get_users()}}.} +} +\value{ +A \code{tibble} with cleaned users data. +} +\description{ +Cleans and un-nests users data. Users data is returned by +\code{\link[=get_users]{get_users()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" + +users <- get_users( + url = url, + username = username, + password = password +) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +clean_users <- clean_users(users = users, language_tokens = language_tokens) +} +} diff --git a/man/contacts_per_case.Rd b/man/contacts_per_case.Rd new file mode 100644 index 0000000..b3f7046 --- /dev/null +++ b/man/contacts_per_case.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/contacts_per_case.R +\name{contacts_per_case} +\alias{contacts_per_case} +\title{Counts the number of contacts per case from relationship data} +\usage{ +contacts_per_case(relationships_clean) +} +\arguments{ +\item{relationships_clean}{A \code{tibble} with the cleaned relationship data. +Relationship data is returned by \code{\link[=get_relationships]{get_relationships()}} and cleaned by +\code{\link[=clean_relationships]{clean_relationships()}}.} +} +\value{ +A \code{tibble} with the number of contacts associated to each source +person +} +\description{ +Uses cleaned relationship data to tally the number of contacts +per case. Relationship data is returned by \code{\link[=get_relationships]{get_relationships()}} and +cleaned by \code{\link[=clean_relationships]{clean_relationships()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +relationships <- get_relationships( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +clean_relationships <- clean_relationships(relationships) + +contacts_per_case <- contacts_per_case(clean_relationships) +} +} diff --git a/man/export_downloader.Rd b/man/export_downloader.Rd index 2558cc4..8cc492d 100644 --- a/man/export_downloader.Rd +++ b/man/export_downloader.Rd @@ -4,17 +4,11 @@ \alias{export_downloader} \title{Function to manage export downloads} \usage{ -export_downloader( - url = url, - username = username, - password = password, - api_call_request = api_call_request, - wait = wait, - file.type = file.type -) +export_downloader(url, username, password, api_call_request, wait, file_type) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} @@ -22,12 +16,17 @@ export_downloader( \item{api_call_request}{The API url to get the number of records.} -\item{wait}{The number of seconds to wait in between iterations of checking the status of the export.} +\item{wait}{The number of seconds to wait in between iterations of checking +the status of the export.} -\item{file.type}{Whether the resulting data frame should contain nested fields (\code{file.type="json"}, the default) or an entirely flat data structure (\code{file.type="csv"})} +\item{file_type}{Whether the resulting data frame should contain nested +fields (\code{file_type = "json"}, the default) or an entirely flat data structure +(\code{file_type = "csv"})} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\verb{\link[tidyr]\{nest\}} for assitance with unnesting. } \description{ A housekeeping function to do export requests & downloads. @@ -39,9 +38,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -cases <- get_cases(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/exposures_per_case.Rd b/man/exposures_per_case.Rd new file mode 100644 index 0000000..51942e5 --- /dev/null +++ b/man/exposures_per_case.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/exposures_per_case.R +\name{exposures_per_case} +\alias{exposures_per_case} +\title{Counts the number of exposures per case from relationship data} +\usage{ +exposures_per_case(relationships_clean) +} +\arguments{ +\item{relationships_clean}{A \code{tibble} with the cleaned relationship data. +Relationship data is returned by \code{\link[=get_relationships]{get_relationships()}} and cleaned by +\code{\link[=clean_relationships]{clean_relationships()}}.} +} +\value{ +A \code{tibble} with the number of exposures associated to each target +person +} +\description{ +Uses cleaned relationship data to tally the number of contacts +per case. Relationship data is returned by \code{\link[=get_relationships]{get_relationships()}} and +cleaned by \code{\link[=clean_relationships]{clean_relationships()}}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +relationships <- get_relationships( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +clean_relationships <- clean_relationships(relationships) + +exposures_per_case <- exposures_per_case(clean_relationships) +} +} diff --git a/man/get_access_token.Rd b/man/get_access_token.Rd index d7df5a4..f504a36 100644 --- a/man/get_access_token.Rd +++ b/man/get_access_token.Rd @@ -4,10 +4,11 @@ \alias{get_access_token} \title{Get an access oauth access token for Go.Data} \usage{ -get_access_token(url = url, username = username, password = password) +get_access_token(url, username, password) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} diff --git a/man/get_active_outbreak.Rd b/man/get_active_outbreak.Rd index 1d93daa..20d79e8 100644 --- a/man/get_active_outbreak.Rd +++ b/man/get_active_outbreak.Rd @@ -4,10 +4,11 @@ \alias{get_active_outbreak} \title{Get the currently active outbreak id number} \usage{ -get_active_outbreak(url = url, username = username, password = password) +get_active_outbreak(url, username, password) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} @@ -30,8 +31,10 @@ url <- "https://MyGoDataServer.com/" username <- "myemail@email.com" password <- "mypassword" -active_outbreak_id <- get_active_outbreak(url=url, - username=username, - password=password) +active_outbreak_id <- get_active_outbreak( + url = url, + username = username, + password = password +) } } diff --git a/man/get_all_outbreaks.Rd b/man/get_all_outbreaks.Rd index 16e9ce3..3e0f6c6 100644 --- a/man/get_all_outbreaks.Rd +++ b/man/get_all_outbreaks.Rd @@ -4,17 +4,20 @@ \alias{get_all_outbreaks} \title{Get a list of all outbreaks and their attributes} \usage{ -get_all_outbreaks(url = url, username = username, password = password) +get_all_outbreaks(url, username, password) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} } \value{ -Returns data frame of outbreaks. The resulting list is filtered by the user's permissions: only outbreaks for which the user has access will be returned. +Returns data frame of outbreaks. The resulting list is filtered by the +user's permissions: only outbreaks for which the user has access will be +returned. } \description{ A function to retrieve all outbreaks assigned @@ -28,8 +31,10 @@ url <- "https://MyGoDataServer.com/" username <- "myemail@email.com" password <- "mypassword" -outbreaks <- get_all_outbreaks(url=url, - username=username, - password=password) +outbreaks <- get_all_outbreaks( + url = url, + username = username, + password = password +) } } diff --git a/man/get_cases.Rd b/man/get_cases.Rd index ca96123..f207f72 100644 --- a/man/get_cases.Rd +++ b/man/get_cases.Rd @@ -5,35 +5,45 @@ \title{Download cases from Go.Data} \usage{ get_cases( - url = url, - username = username, - password = password, - outbreak_id = outbreak_id, + url, + username, + password, + outbreak_id, method = c("export", "batch"), batch_size = 50000, wait = 2, - file.type = c("json", "csv") + file_type = c("json", "csv") ) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. +Don't forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} -\item{outbreak_id}{The id number for the outbreak for which you want to download data.} +\item{outbreak_id}{The id number for the outbreak for which you want to +download data.} -\item{method}{The method to download data. \code{method="export"} is the preferred and default method for Go.Data version 2.38.1 or later. See Details.} +\item{method}{The method to download data. \code{method = "export"} is the +preferred and default method for Go.Data version 2.38.1 or later. +See Details.} -\item{batch_size}{If \code{method="batches"}, then \code{batch_size} specifies the number of records to retrieve in each iteration.} +\item{batch_size}{If \code{method = "batches"}, then \code{batch_size} specifies the +number of records to retrieve in each iteration.} -\item{wait}{If \code{method="export"}, then \code{wait} is the number of seconds to wait in between iterations of checking the status of the export.} +\item{wait}{If \code{method = "export"}, then \code{wait} is the number of seconds to +wait in between iterations of checking the status of the export.} -\item{file.type}{If \code{method="export"}, then \code{file.type} determines Whether the resulting data frame should contain nested fields (\code{file.type="json"}, the default) or an entirely flat data structure (\code{file.type="csv"})} +\item{file_type}{If \code{method = "export"}, then \code{file_type} determines Whether +the resulting data frame should contain nested fields (\code{file_type = "json"}, +the default) or an entirely flat data structure (\code{file_type = "csv"})} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\verb{\link[tidyr]\{nest\}} for assitance with unnesting. } \description{ A function to retrieve the case data for a @@ -47,7 +57,7 @@ are two methods for downloading the data: Go.Data. This method relies on the GET outbreak/{id}/cases API endpoint. Records are then retrieved in batches based on \code{batch_size} and appended together into -a final dataset. \code{method="batches"} will be the default and +a final dataset. \code{method = "batches"} will be the default and only available method for Go.Data version 2.38.0 or older. \code{method="export"} will only work on Go.Data versions @@ -55,7 +65,7 @@ only available method for Go.Data version 2.38.0 or older. outbreak/{id}/cases/export API endpoint. An export request is submitted to the server, and then when the export is ready, it will be downloaded. Due to better -performance and more options, \code{method="export"} will +performance and more options, \code{method = "export"} will be the default if you are using Go.Data version 2.38.1 or newer. } @@ -66,9 +76,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -cases <- get_cases(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/get_cases_questionnaire.Rd b/man/get_cases_questionnaire.Rd new file mode 100644 index 0000000..658dd83 --- /dev/null +++ b/man/get_cases_questionnaire.Rd @@ -0,0 +1,53 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_cases_questionnaire.R +\name{get_cases_questionnaire} +\alias{get_cases_questionnaire} +\title{Download cases from Go.Data and returns questionnaire fields} +\usage{ +get_cases_questionnaire(url, username, password, outbreak_id, wait = 2) +} +\arguments{ +\item{url}{Insert the base URL for your instance of Go.Data here. +Don't forget the forward slash "/" at end!} + +\item{username}{The email address for your Go.Data login.} + +\item{password}{The password for your Go.Data login} + +\item{outbreak_id}{The id number for the outbreak for which you want to +download data.} + +\item{wait}{If \code{method = "export"}, then \code{wait} is the number of seconds to +wait in between iterations of checking the status of the export.} +} +\value{ +Returns a \code{tibble}. +} +\description{ +A function that retrieves the questionnaire fields from case data for a +specific \code{outbreak_id}. +} +\details{ +Unlike \code{\link[=get_cases]{get_cases()}} this function only uses the \code{\link[=export_downloader]{export_downloader()}}, +and not the \code{\link[=batch_downloader]{batch_downloader()}}. Therefore, this function will only work +on Go.Data versions 2.38.1 or newer. This method relies on the GET +outbreak/{id}/cases/export API endpoint. An export request is submitted to +the server, and then when the export is ready, it will be downloaded. + +This function fixes the file return type to \code{"csv"}. +} +\examples{ +\dontrun{ +url <- "https://MyGoDataServer.com/" +username <- "myemail@email.com" +password <- "mypassword" +outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" + +cases <- get_cases_questionnaire( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) +} +} diff --git a/man/get_clusters.Rd b/man/get_clusters.Rd index a629ae9..550a938 100644 --- a/man/get_clusters.Rd +++ b/man/get_clusters.Rd @@ -4,24 +4,21 @@ \alias{get_clusters} \title{Download clusters from Go.Data (version agnostic)} \usage{ -get_clusters( - url = url, - username = username, - password = password, - outbreak_id = outbreak_id, - batch_size = 50000 -) +get_clusters(url, username, password, outbreak_id, batch_size = 50000) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} -\item{outbreak_id}{The id number for the outbreak for which you want to download clusters.} +\item{outbreak_id}{The id number for the outbreak for which you want to +download clusters.} -\item{batch_size}{For large datasets, specify the number of records to retrieve in each iteration.} +\item{batch_size}{For large datasets, specify the number of records to +retrieve in each iteration.} } \value{ Returns data frame. @@ -44,9 +41,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -clusters <- get_clusters(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +clusters <- get_clusters( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/get_contacts.Rd b/man/get_contacts.Rd index 68817ca..3e9389e 100644 --- a/man/get_contacts.Rd +++ b/man/get_contacts.Rd @@ -5,35 +5,45 @@ \title{Download contacts from Go.Data} \usage{ get_contacts( - url = url, - username = username, - password = password, - outbreak_id = outbreak_id, + url, + username, + password, + outbreak_id, method = c("export", "batch"), batch_size = 50000, wait = 2, - file.type = c("json", "csv") + file_type = c("json", "csv") ) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} -\item{outbreak_id}{The id number for the outbreak for which you want to download data.} +\item{outbreak_id}{The id number for the outbreak for which you want to +download data.} -\item{method}{The method to download data. \code{method="export"} is the preferred and default method for Go.Data version 2.38.1 or later. See Details.} +\item{method}{The method to download data. \code{method="export"} is the +preferred and default method for Go.Data version 2.38.1 or later. See +Details.} -\item{batch_size}{If \code{method="batches"}, then \code{batch_size} specifies the number of records to retrieve in each iteration.} +\item{batch_size}{If \code{method = "batches"}, then \code{batch_size} specifies the +number of records to retrieve in each iteration.} -\item{wait}{If \code{method="export"}, then \code{wait} is the number of seconds to wait in between iterations of checking the status of the export.} +\item{wait}{If \code{method = "export"}, then \code{wait} is the number of seconds to +wait in between iterations of checking the status of the export.} -\item{file.type}{If \code{method="export"}, then \code{file.type} determines Whether the resulting data frame should contain nested fields (\code{file.type="json"}, the default) or an entirely flat data structure (\code{file.type="csv"})} +\item{file_type}{If \code{method="export"}, then \code{file_type} determines Whether +the resulting data frame should contain nested fields (\code{file_type = "json"}, +the default) or an entirely flat data structure (\code{file_type = "csv"})} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\code{\link[tidyr]{nest}} for assitance with unnesting. } \description{ A function to retrieve the contact data for a @@ -43,19 +53,19 @@ specific \code{outbreak_id}. This function works on all versions of Go.Data. There are two methods for downloading the data: -\code{method="batches"} will work on all versions of +\code{method = "batches"} will work on all versions of Go.Data. This method relies on the GET outbreak/{id}/contacts API endpoint. Records are then retrieved in batches based on \code{batch_size} and appended together into -a final dataset. \code{method="batches"} will be the default and +a final dataset. \code{method = "batches"} will be the default and only available method for Go.Data version 2.38.0 or older. -\code{method="export"} will only work on Go.Data versions +\code{method = "export"} will only work on Go.Data versions 2.38.1 or newer. This method relies on the GET outbreak/{id}/contacts/export API endpoint. An export request is submitted to the server, and then when the export is ready, it will be downloaded. Due to better -performance and more options, \code{method="export"} will +performance and more options, \code{method = "export"} will be the default if you are using Go.Data version 2.38.1 or newer. } @@ -66,9 +76,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -contacts <- get_contacts(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +contacts <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id=outbreak_id +) } } diff --git a/man/get_contacts_of_contacts.Rd b/man/get_contacts_of_contacts.Rd index 66bc41f..33d8205 100644 --- a/man/get_contacts_of_contacts.Rd +++ b/man/get_contacts_of_contacts.Rd @@ -5,35 +5,45 @@ \title{Download contacts-of-contacts from Go.Data} \usage{ get_contacts_of_contacts( - url = url, - username = username, - password = password, - outbreak_id = outbreak_id, + url, + username, + password, + outbreak_id, method = c("export", "batch"), batch_size = 50000, wait = 2, - file.type = c("json", "csv") + file_type = c("json", "csv") ) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} -\item{outbreak_id}{The id number for the outbreak for which you want to download data.} +\item{outbreak_id}{The id number for the outbreak for which you want to +download data.} -\item{method}{The method to download data. \code{method="export"} is the preferred and default method for Go.Data version 2.38.1 or later. See Details.} +\item{method}{The method to download data. \code{method = "export"} is the +preferred and default method for Go.Data version 2.38.1 or later. See +Details.} -\item{batch_size}{If \code{method="batches"}, then \code{batch_size} specifies the number of records to retrieve in each iteration.} +\item{batch_size}{If \code{method = "batches"}, then \code{batch_size} specifies the +number of records to retrieve in each iteration.} -\item{wait}{If \code{method="export"}, then \code{wait} is the number of seconds to wait in between iterations of checking the status of the export.} +\item{wait}{If \code{method = "export"}, then \code{wait} is the number of seconds to +wait in between iterations of checking the status of the export.} -\item{file.type}{If \code{method="export"}, then \code{file.type} determines Whether the resulting data frame should contain nested fields (\code{file.type="json"}, the default) or an entirely flat data structure (\code{file.type="csv"})} +\item{file_type}{If \code{method = "export"}, then \code{file_type} determines Whether +the resulting data frame should contain nested fields (\code{file_type = "json"}, +the default) or an entirely flat data structure (\code{file_type = "csv"})} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\code{\link[tidyr]{nest}} for assitance with unnesting. } \description{ A function to retrieve the contact-of-contact @@ -43,20 +53,20 @@ data for a specific \code{outbreak_id}. This function works on all versions of Go.Data. There are two methods for downloading the data: -\code{method="batches"} will work on all versions of +\code{method = "batches"} will work on all versions of Go.Data. This method relies on the GET outbreak/{id}/contacts-of-contacts API endpoint. Records are then retrieved in batches based on \code{batch_size} and appended together into a final -dataset. \code{method="batches"} will be the default and +dataset. \code{method = "batches"} will be the default and only available method for Go.Data version 2.38.0 or older. -\code{method="export"} will only work on Go.Data versions +\code{method = "export"} will only work on Go.Data versions 2.38.1 or newer. This method relies on the GET outbreak/{id}/contacts-of-contacts/export API endpoint. An export request is submitted to the server, and then when the export is ready, it will be downloaded. Due to -better performance and more options, \code{method="export"} will +better performance and more options, \code{method = "export"} will be the default if you are using Go.Data version 2.38.1 or newer. } @@ -67,9 +77,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -contacts_of_contacts <- get_contacts_of_contacts(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +contacts_of_contacts <- get_contacts_of_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/get_events.Rd b/man/get_events.Rd index 6985f16..f7ad66f 100644 --- a/man/get_events.Rd +++ b/man/get_events.Rd @@ -5,35 +5,45 @@ \title{Download events from Go.Data} \usage{ get_events( - url = url, - username = username, - password = password, - outbreak_id = outbreak_id, + url, + username, + password, + outbreak_id, method = c("export", "batch"), batch_size = 50000, wait = 2, - file.type = c("json", "csv") + file_type = c("json", "csv") ) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} -\item{outbreak_id}{The id number for the outbreak for which you want to download data.} +\item{outbreak_id}{The id number for the outbreak for which you want to +download data.} -\item{method}{The method to download data. \code{method="export"} is the preferred and default method for Go.Data version 2.38.1 or later. See Details.} +\item{method}{The method to download data. \code{method = "export"} is the +preferred and default method for Go.Data version 2.38.1 or later. See +Details.} -\item{batch_size}{If \code{method="batches"}, then \code{batch_size} specifies the number of records to retrieve in each iteration.} +\item{batch_size}{If \code{method = "batches"}, then \code{batch_size} specifies the +number of records to retrieve in each iteration.} -\item{wait}{If \code{method="export"}, then \code{wait} is the number of seconds to wait in between iterations of checking the status of the export.} +\item{wait}{If \code{method = "export"}, then \code{wait} is the number of seconds to +wait in between iterations of checking the status of the export.} -\item{file.type}{If \code{method="export"}, then \code{file.type} determines Whether the resulting data frame should contain nested fields (\code{file.type="json"}, the default) or an entirely flat data structure (\code{file.type="csv"})} +\item{file_type}{If \code{method = "export"}, then \code{file_type} determines Whether +the resulting data frame should contain nested fields (\code{file_type = "json"}, +the default) or an entirely flat data structure (\code{file_type = "csv"})} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\code{\link[tidyr]{nest}} for assitance with unnesting. } \description{ A function to retrieve the event data for a @@ -43,19 +53,19 @@ specific \code{outbreak_id}. This function works on all versions of Go.Data. There are two methods for downloading the data: -\code{method="batches"} will work on all versions of +\code{method = "batches"} will work on all versions of Go.Data. This method relies on the GET outbreak/{id}/events API endpoint. Records are then retrieved in batches based on \code{batch_size} and appended together into -a final dataset. \code{method="batches"} will be the default and +a final dataset. \code{method = "batches"} will be the default and only available method for Go.Data version 2.38.0 or older. -\code{method="export"} will only work on Go.Data versions +\code{method = "export"} will only work on Go.Data versions 2.38.1 or newer. This method relies on the GET outbreak/{id}/events/export API endpoint. An export request is submitted to the server, and then when the export is ready, it will be downloaded. Due to better -performance and more options, \code{method="export"} will +performance and more options, \code{method = "export"} will be the default if you are using Go.Data version 2.38.1 or newer. } @@ -66,9 +76,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -events <- get_events(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +events <- get_events( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/get_export_status.Rd b/man/get_export_status.Rd index 47e7a51..b891e7d 100644 --- a/man/get_export_status.Rd +++ b/man/get_export_status.Rd @@ -4,15 +4,11 @@ \alias{get_export_status} \title{Check the status of an export request from Go.Data (version 2.38.1 or later)} \usage{ -get_export_status( - url = url, - username = username, - password = password, - request_id = request_id -) +get_export_status(url, username, password, request_id) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} @@ -34,22 +30,33 @@ url <- "https://MyGoDataServer.com/" username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -access_token <- get_access_token(url=url, - username=username, - password=password) +access_token <- get_access_token( + url = url, + username = username, + password = password +) -#Submit an export request -export.request <- GET(paste0(url,"api/outbreaks/",outbreak_id,"/cases/export", - "&access_token=",access_token)) -request_id <- export.request \%>\% - content() \%>\% - pluck("exportLogId") +# Submit an export request +export_request <- GET( + paste0( + url, + "api/outbreaks/", + outbreak_id, + "/cases/export", + "&access_token=", + access_token + ) +) +request_id <- content(export_request) +request_id <- pluck(request_id, "exportLogId") -#Check the status of the export request +# Check the status of the export request -export.request.status <- get_export_status(url=url, - username=username, - password=password, - request_id=request_id) +export_request_status <- get_export_status( + url = url, + username = username, + password = password, + request_id = request_id +) } } diff --git a/man/get_followups.Rd b/man/get_followups.Rd index 82b6fcc..75fd1b8 100644 --- a/man/get_followups.Rd +++ b/man/get_followups.Rd @@ -5,35 +5,45 @@ \title{Download contact follow-ups from Go.Data} \usage{ get_followups( - url = url, - username = username, - password = password, - outbreak_id = outbreak_id, + url, + username, + password, + outbreak_id, method = c("export", "batch"), batch_size = 50000, wait = 2, - file.type = c("json", "csv") + file_type = c("json", "csv") ) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} -\item{outbreak_id}{The id number for the outbreak for which you want to download data.} +\item{outbreak_id}{The id number for the outbreak for which you want to +download data.} -\item{method}{The method to download data. \code{method="export"} is the preferred and default method for Go.Data version 2.38.1 or later. See Details.} +\item{method}{The method to download data. \code{method = "export"} is the +preferred and default method for Go.Data version 2.38.1 or later. See +Details.} -\item{batch_size}{If \code{method="batches"}, then \code{batch_size} specifies the number of records to retrieve in each iteration.} +\item{batch_size}{If \code{method = "batches"}, then \code{batch_size} specifies the +number of records to retrieve in each iteration.} -\item{wait}{If \code{method="export"}, then \code{wait} is the number of seconds to wait in between iterations of checking the status of the export.} +\item{wait}{If \code{method = "export"}, then \code{wait} is the number of seconds to +wait in between iterations of checking the status of the export.} -\item{file.type}{If \code{method="export"}, then \code{file.type} determines Whether the resulting data frame should contain nested fields (\code{file.type="json"}, the default) or an entirely flat data structure (\code{file.type="csv"})} +\item{file_type}{If \code{method = "export"}, then \code{file_type} determines Whether +the resulting data frame should contain nested fields (\code{file_type = "json"}, +the default) or an entirely flat data structure (\code{file_type = "csv"})} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\code{\link[tidyr]{nest}} for assitance with unnesting. } \description{ A function to retrieve the follow-up data for a @@ -43,19 +53,19 @@ specific \code{outbreak_id}. This function works on all versions of Go.Data. There are two methods for downloading the data: -\code{method="batches"} will work on all versions of +\code{method = "batches"} will work on all versions of Go.Data. This method relies on the GET outbreak/{id}/follow-ups API endpoint. Records are then retrieved in batches based on \code{batch_size} and appended together into -a final dataset. \code{method="batches"} will be the default and +a final dataset. \code{method = "batches"} will be the default and only available method for Go.Data version 2.38.0 or older. -\code{method="export"} will only work on Go.Data versions +\code{method = "export"} will only work on Go.Data versions 2.38.1 or newer. This method relies on the GET outbreak/{id}/follow-ups/export API endpoint. An export request is submitted to the server, and then when the export is ready, it will be downloaded. Due to better -performance and more options, \code{method="export"} will +performance and more options, \code{method = "export"} will be the default if you are using Go.Data version 2.38.1 or newer. } @@ -66,9 +76,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -followups <- get_followups(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +followups <- get_followups( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/get_godata_version.Rd b/man/get_godata_version.Rd index 809e38f..db8cd2a 100644 --- a/man/get_godata_version.Rd +++ b/man/get_godata_version.Rd @@ -7,7 +7,8 @@ get_godata_version(url = url) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} } \value{ string @@ -21,6 +22,6 @@ other \code{godataR} functions. \examples{ \dontrun{ url <- "https://MyGoDataServer.com/" -get_godata_version(url=url) +get_godata_version(url = url) } } diff --git a/man/get_labresults.Rd b/man/get_labresults.Rd index 4dbac8d..3fc40cc 100644 --- a/man/get_labresults.Rd +++ b/man/get_labresults.Rd @@ -5,35 +5,45 @@ \title{Download lab results from Go.Data} \usage{ get_labresults( - url = url, - username = username, - password = password, - outbreak_id = outbreak_id, + url, + username, + password, + outbreak_id, method = c("export", "batch"), batch_size = 50000, wait = 2, - file.type = c("json", "csv") + file_type = c("json", "csv") ) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} -\item{outbreak_id}{The id number for the outbreak for which you want to download data.} +\item{outbreak_id}{The id number for the outbreak for which you want to +download data.} -\item{method}{The method to download data. \code{method="export"} is the preferred and default method for Go.Data version 2.38.1 or later. See Details.} +\item{method}{The method to download data. \code{method="export"} is the +preferred and default method for Go.Data version 2.38.1 or later. See +Details.} -\item{batch_size}{If \code{method="batches"}, then \code{batch_size} specifies the number of records to retrieve in each iteration.} +\item{batch_size}{If \code{method = "batches"}, then \code{batch_size} specifies the +number of records to retrieve in each iteration.} -\item{wait}{If \code{method="export"}, then \code{wait} is the number of seconds to wait in between iterations of checking the status of the export.} +\item{wait}{If \code{method = "export"}, then \code{wait} is the number of seconds to +wait in between iterations of checking the status of the export.} -\item{file.type}{If \code{method="export"}, then \code{file.type} determines Whether the resulting data frame should contain nested fields (\code{file.type="json"}, the default) or an entirely flat data structure (\code{file.type="csv"})} +\item{file_type}{If \code{method = "export"}, then \code{file_type} determines Whether +the resulting data frame should contain nested fields (\code{file_type = "json"}, +the default) or an entirely flat data structure (\code{file_type = "csv"})} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\code{\link[tidyr]{nest}} for assitance with unnesting. } \description{ A function to retrieve the lab result data for a @@ -66,9 +76,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -labresults <- get_labresults(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +labresults <- get_labresults( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/get_language_tokens.Rd b/man/get_language_tokens.Rd index f40f3f3..d6ab57c 100644 --- a/man/get_language_tokens.Rd +++ b/man/get_language_tokens.Rd @@ -2,29 +2,29 @@ % Please edit documentation in R/get_language_tokens.R \name{get_language_tokens} \alias{get_language_tokens} -\title{Get a list of variable tokens and their labels for the language you specify, in order to re-code variables in R.} +\title{Get a list of variable tokens and their labels for the language you specify, +in order to re-code variables in R.} \usage{ -get_language_tokens( - url = url, - username = username, - password = password, - language = language -) +get_language_tokens(url, username, password, language) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} -\item{language}{The language ID you are retrieving translation file for, for instance "english_us"} +\item{language}{The language ID you are retrieving translation file for, +for instance "english_us"} } \value{ -Returns data frame of language tokens for your language. You will only be able to execute this function if you have access to the language tokens. +Returns data frame of language tokens for your language. You will only be +able to execute this function if you have access to the language tokens. } \description{ -Get a list of variable tokens and their labels for the language you specify, in order to re-code variables in R. +Get a list of variable tokens and their labels for the language you specify, +in order to re-code variables in R. } \examples{ \dontrun{ @@ -33,9 +33,11 @@ username <- "myemail@email.com" password <- "mypassword" language <- "english_us" -language_tokens <- get_language_tokens(url=url, - username=username, - password=password, - language=language) +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = language +) } } diff --git a/man/get_languages.Rd b/man/get_languages.Rd index 2790301..003e55d 100644 --- a/man/get_languages.Rd +++ b/man/get_languages.Rd @@ -4,10 +4,11 @@ \alias{get_languages} \title{Get lanuages in Go.Data} \usage{ -get_languages(url = url, username = username, password = password) +get_languages(url, username, password) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} @@ -26,8 +27,10 @@ username <- "myemail@email.com" password <- "mypassword" language <- "english_us" -languages <- get_languages(url=url, - username=username, - password=password) +languages <- get_languages( + url = url, + username = username, + password = password +) } } diff --git a/man/get_locations.Rd b/man/get_locations.Rd index 67cb4db..77b1f98 100644 --- a/man/get_locations.Rd +++ b/man/get_locations.Rd @@ -4,10 +4,11 @@ \alias{get_locations} \title{Get location data from Go.Data} \usage{ -get_locations(url = url, username = username, password = password) +get_locations(url, username, password) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} @@ -29,8 +30,10 @@ url <- "https://MyGoDataServer.com/" username <- "myemail@email.com" password <- "mypassword" -locations <- get_locations(url=url, - username=username, - password=password) +locations <- get_locations( + url = url, + username = username, + password = password +) } } diff --git a/man/get_reference_data.Rd b/man/get_reference_data.Rd index 2b42b35..e21b311 100644 --- a/man/get_reference_data.Rd +++ b/man/get_reference_data.Rd @@ -4,10 +4,11 @@ \alias{get_reference_data} \title{Get reference data from Go.Data} \usage{ -get_reference_data(url = url, username = username, password = password) +get_reference_data(url, username, password) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} @@ -29,8 +30,10 @@ url <- "https://MyGoDataServer.com/" username <- "myemail@email.com" password <- "mypassword" -reference_data <- get_reference_data(url=url, - username=username, - password=password) +reference_data <- get_reference_data( + url = url, + username = username, + password = password +) } } diff --git a/man/get_relationships.Rd b/man/get_relationships.Rd index f485fe8..f6f3a6b 100644 --- a/man/get_relationships.Rd +++ b/man/get_relationships.Rd @@ -5,35 +5,45 @@ \title{Download relationships from Go.Data} \usage{ get_relationships( - url = url, - username = username, - password = password, - outbreak_id = outbreak_id, + url, + username, + password, + outbreak_id, method = c("export", "batch"), batch_size = 50000, wait = 2, - file.type = c("json", "csv") + file_type = c("json", "csv") ) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} -\item{outbreak_id}{The id number for the outbreak for which you want to download data.} +\item{outbreak_id}{The id number for the outbreak for which you want to +download data.} -\item{method}{The method to download data. \code{method="export"} is the preferred and default method for Go.Data version 2.38.1 or later. See Details.} +\item{method}{The method to download data. \code{method = "export"} is the +preferred and default method for Go.Data version 2.38.1 or later. See +Details.} -\item{batch_size}{If \code{method="batches"}, then \code{batch_size} specifies the number of records to retrieve in each iteration.} +\item{batch_size}{If \code{method = "batches"}, then \code{batch_size} specifies the +number of records to retrieve in each iteration.} -\item{wait}{If \code{method="export"}, then \code{wait} is the number of seconds to wait in between iterations of checking the status of the export.} +\item{wait}{If \code{method = "export"}, then \code{wait} is the number of seconds to +wait in between iterations of checking the status of the export.} -\item{file.type}{If \code{method="export"}, then \code{file.type} determines Whether the resulting data frame should contain nested fields (\code{file.type="json"}, the default) or an entirely flat data structure (\code{file.type="csv"})} +\item{file_type}{If \code{method = "export"}, then \code{file_type} determines Whether +the resulting data frame should contain nested fields (\code{file_type = "json"}, +the default) or an entirely flat data structure (\code{file_type = "csv"})} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\code{\link[tidyr]{nest}} for assitance with unnesting. } \description{ A function to retrieve the relationship data for a @@ -43,19 +53,19 @@ specific \code{outbreak_id}. This function works on all versions of Go.Data. There are two methods for downloading the data: -\code{method="batches"} will work on all versions of +\code{method = "batches"} will work on all versions of Go.Data. This method relies on the GET outbreak/{id}/relationships API endpoint. Records are then retrieved in batches based on \code{batch_size} and appended together into -a final dataset. \code{method="batches"} will be the default and +a final dataset. \code{method = "batches"} will be the default and only available method for Go.Data version 2.38.0 or older. -\code{method="export"} will only work on Go.Data versions +\code{method = "export"} will only work on Go.Data versions 2.38.1 or newer. This method relies on the GET outbreak/{id}/relationships/export API endpoint. An export request is submitted to the server, and then when the export is ready, it will be downloaded. Due to better -performance and more options, \code{method="export"} will +performance and more options, \code{method = "export"} will be the default if you are using Go.Data version 2.38.1 or newer. } @@ -66,9 +76,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -relationships <- get_relationships(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +relationships <- get_relationships( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/get_teams.Rd b/man/get_teams.Rd index add05f6..2879e2d 100644 --- a/man/get_teams.Rd +++ b/man/get_teams.Rd @@ -4,10 +4,11 @@ \alias{get_teams} \title{#' Get teams data from Go.Data} \usage{ -get_teams(url = url, username = username, password = password) +get_teams(url, username, password) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} @@ -29,8 +30,10 @@ url <- "https://MyGoDataServer.com/" username <- "myemail@email.com" password <- "mypassword" -teams <- get_teams(url=url, - username=username, - password=password) +teams <- get_teams( + url = url, + username = username, + password = password +) } } diff --git a/man/get_users.Rd b/man/get_users.Rd index 0710668..69f4cb8 100644 --- a/man/get_users.Rd +++ b/man/get_users.Rd @@ -4,17 +4,20 @@ \alias{get_users} \title{Get user data from Go.Data} \usage{ -get_users(url = url, username = username, password = password) +get_users(url, username, password) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} \item{password}{The password for your Go.Data login} } \value{ -Returns a data frame. Some fields, such as addresses, hospitalization history, and questionnaire fields may require further unnesting. See \code{\link[tidyr]{nest}} for assitance with unnesting. +Returns a data frame. Some fields, such as addresses, hospitalization +history, and questionnaire fields may require further unnesting. See +\code{\link[tidyr]{nest}} for assitance with unnesting. } \description{ A function to retrieve a list of all user @@ -29,8 +32,10 @@ url <- "https://MyGoDataServer.com/" username <- "myemail@email.com" password <- "mypassword" -users <- get_users(url=url, - username=username, - password=password) +users <- get_users( + url = url, + username = username, + password = password +) } } diff --git a/man/mongify_date.Rd b/man/mongify_date.Rd index 59bde2d..26f79bf 100644 --- a/man/mongify_date.Rd +++ b/man/mongify_date.Rd @@ -49,7 +49,8 @@ element order can be selected (i.e. all dates in the column to be converted must be in the same format). Dates with and without a time-stamp are both accepted. Dates which do include -a time stamp must include hours, minutes and seconds in the format \strong{HH:mm:ss} +a time stamp must include hours, minutes and seconds in the format +\strong{HH:mm:ss} } \examples{ # Create dummy dataframe with dates to convert: diff --git a/man/set_active_outbreak.Rd b/man/set_active_outbreak.Rd index 8ffbc8a..549b038 100644 --- a/man/set_active_outbreak.Rd +++ b/man/set_active_outbreak.Rd @@ -4,15 +4,11 @@ \alias{set_active_outbreak} \title{Change the currently active outbreak} \usage{ -set_active_outbreak( - url = url, - username = username, - password = password, - outbreak_id = outbreak_id -) +set_active_outbreak(url, username, password, outbreak_id) } \arguments{ -\item{url}{Insert the base URL for your instance of Go.Data here. Don't forget the forward slash "/" at end!} +\item{url}{Insert the base URL for your instance of Go.Data here. Don't +forget the forward slash "/" at end!} \item{username}{The email address for your Go.Data login.} @@ -40,9 +36,11 @@ username <- "myemail@email.com" password <- "mypassword" outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b" -set_active_outbreak(url=url, - username=username, - password=password, - outbreak_id=outbreak_id) +set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) } } diff --git a/man/translate_categories.Rd b/man/translate_categories.Rd new file mode 100644 index 0000000..d6fdc1b --- /dev/null +++ b/man/translate_categories.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/translate_categories.R +\name{translate_categories} +\alias{translate_categories} +\title{Translates cateogories with API labels to more readable forms using the +translation specified in the output of \code{get_language_tokens()}.} +\usage{ +translate_categories(data, language_tokens) +} +\arguments{ +\item{data}{A data frame (or data frame extension)} + +\item{language_tokens}{A data frame (or data frame extension) containing +the translations. Output from \code{get_language_tokens()}} +} +\value{ +A tibble +} +\description{ +Translates cateogories with API labels to more readable forms using the +translation specified in the output of \code{get_language_tokens()}. +} +\examples{ +\dontrun{ +cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id +) + +language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" +) + +translate_categories( + data = cases, + language_tokens = language_tokens +) +} +} diff --git a/man/translate_token.Rd b/man/translate_token.Rd new file mode 100644 index 0000000..4be70c6 --- /dev/null +++ b/man/translate_token.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/translate_categories.R +\name{translate_token} +\alias{translate_token} +\title{Translates tokens given a specified translation.} +\usage{ +translate_token(data, language_tokens) +} +\arguments{ +\item{data}{A data frame (or data frame extension)} + +\item{language_tokens}{A data frame (or data frame extension) containing +the translations. Output from \code{get_language_tokens()}} +} +\value{ +A tibble +} +\description{ +This function does all the translation for +\code{translate_categories}. +} +\keyword{internal} diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..7427e8c --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/tests.html +# * https://testthat.r-lib.org/reference/test_package.html#special-files + +library(testthat) +library(godataR) + +test_check("godataR") diff --git a/tests/testthat/test-batch_downloader.R b/tests/testthat/test-batch_downloader.R new file mode 100644 index 0000000..52daf07 --- /dev/null +++ b/tests/testthat/test-batch_downloader.R @@ -0,0 +1,157 @@ +test_that("batch_downloader works as expected", { + skip("batch_downloader requires API call") + + api_call_n <- paste0(url, "api/outbreaks/", outbreak_id, "/cases/count") + api_call_get <- paste0(url, "api/outbreaks/", outbreak_id, "/cases") + res <- batch_downloader( + url = url, + username = username, + password = password, + api_call_n = api_call_n, + api_call_get = api_call_get, + batch_size = 50000 + ) + + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(14L, 153L)) + expect_identical( + colnames(res), + c("firstName", "gender", "wasContact", "safeBurial", "classification", + "investigationStatus", "riskLevel", "transferRefused", "vaccinesReceived", + "id", "outbreakId", "visualId", "lastName", "dob", "occupation", + "documents", "addresses", "dateOfReporting", + "isDateOfReportingApproximate", "dateOfLastContact", "dateOfOnset", + "dateRanges", "classificationHistory", "hasRelationships", + "relationshipsRepresentation", "numberOfExposures", "numberOfContacts", + "usualPlaceOfResidenceLocationId", "responsibleUserId", "createdAt", + "createdBy", "updatedAt", "updatedBy", "createdOn", "deleted", + "dateBecomeCase", "wasCase", "active", "followUpHistory", + "isDateOfOnsetApproximate", "outcomeId", "riskReason", "pregnancyStatus", + "dateOfOutcome", "dateOfInfection", "middleName", + "questionnaireAnswers.would_you_like_to_complete_who_basic_case_questionnaire", # nolint + "questionnaireAnswers.Case_WhichForm", + "questionnaireAnswers.report_test_reason", + "questionnaireAnswers.Comcond_present", + "questionnaireAnswers.patinfo_occuhcw", + "questionnaireAnswers.expo_travel", + "questionnaireAnswers.expo_visit_healthcare", + "questionnaireAnswers.patcourse", "questionnaireAnswers.Comcond_select", + "questionnaireAnswers.specify_places_and_dates_for_up_to_3_locations_below", # nolint + "questionnaireAnswers.expo_travel_country1", + "questionnaireAnswers.expo_travel_city1", + "questionnaireAnswers.expo_travel_date1", + "questionnaireAnswers.FA0_UniqueIDClustNumber", + "questionnaireAnswers.FA0_datacollector_name", + "questionnaireAnswers.FA0_datacollector_institution", + "questionnaireAnswers.FA0_datacollector_telephone", + "questionnaireAnswers.FA0_datacollector_email", + "questionnaireAnswers.FA0_caseidentifier_email", + "questionnaireAnswers.FA0_caseidentifier_socialnumber", + "questionnaireAnswers.FA0_case_countryresidence", + "questionnaireAnswers.FA0_respondent_ispatient", + "questionnaireAnswers.FA0_symptoms_caseshowssymptoms", + "questionnaireAnswers.FA0_respiratorysample_collectedYN", + "questionnaireAnswers.FA0_clinicalcomplications_ARDS", + "questionnaireAnswers.FA0_clinicalcomplications_pneumoniachestXray", + "questionnaireAnswers.FA0_symptom_fever", + "questionnaireAnswers.FA0_symptom_sorethroat", + "questionnaireAnswers.FA0_symptom_runnynose", + "questionnaireAnswers.FA0_symptom_cough", + "questionnaireAnswers.FA0_symptom_shortnessofbreath", + "questionnaireAnswers.FA0_symptom_vomiting", + "questionnaireAnswers.FA0_symptom_nausea", + "questionnaireAnswers.FA0_symptom_diarrhea", + "questionnaireAnswers.FA0_symptom_taste", "questionnaireAnswers.test", + "questionnaireAnswers.which_date_omicron_was_found", + "questionnaireAnswers.place_of_death", + "questionnaireAnswers.patinfo_occuhcw_country", + "questionnaireAnswers.patinfo_occuhcw_city", + "questionnaireAnswers.patinfo_occuhcw_name", + "questionnaireAnswers.country_2", + "questionnaireAnswers.city_2", "questionnaireAnswers.date_2", + "questionnaireAnswers.FA1_UniqueIDClustNumber", + "questionnaireAnswers.FA1_FurtherCaseClassification", + "questionnaireAnswers.FA1_datacollector_name", + "questionnaireAnswers.FA1_datacollector_institution", + "questionnaireAnswers.FA1_datacollector_telephone", + "questionnaireAnswers.FA1_datacollector_email", + "questionnaireAnswers.FA1_respondent_ispatient", + "questionnaireAnswers.FA1_caseidentifier_email", + "questionnaireAnswers.FA1_caseidentifier_socialnumber", + "questionnaireAnswers.FA1_case_countryresidence", + "questionnaireAnswers.FA1_case_nationality", + "questionnaireAnswers.FA1_case_ethnicity", + "questionnaireAnswers.FA1_case_responsiblehealthcentre", + "questionnaireAnswers.FA1_case_nurseryschoolcollege", + "questionnaireAnswers.FA1_carecentre_practicename", + "questionnaireAnswers.FA1_carecentre_treatingphysicianname", + "questionnaireAnswers.FA1_carecentre_casepartofoutbreak", + "questionnaireAnswers.FA1_carecentre_telephone", + "questionnaireAnswers.FA1_carecentre_fax", + "questionnaireAnswers.FA1_carecentre_address", + "questionnaireAnswers.FA1_symptoms_caseshowssymptoms", + "questionnaireAnswers.FA1_symptoms_healthfacilityvisitedYN", + "questionnaireAnswers.FA1_complications_mechanicalventilation", + "questionnaireAnswers.FA1_complications_ARDS", + "questionnaireAnswers.FA1_complications_acuterenalfailure", + "questionnaireAnswers.FA1_complications_cardiacfailure", + "questionnaireAnswers.FA1_complications_consumptivecoagulopathy", + "questionnaireAnswers.FA1_complications_pneumoniachestXray", + "questionnaireAnswers.FA1_complications_other", + "questionnaireAnswers.FA1_complications_EMOrequired", + "questionnaireAnswers.FA1_complications_hypotensionrequiringvasopressors", + "questionnaireAnswers.FA1_preexistingconditions_obesity", + "questionnaireAnswers.FA1_preexistingconditions_cancer", + "questionnaireAnswers.FA1_preexistingconditions_diabetes", + "questionnaireAnswers.FA1_preexistingconditions_HIVotherimmunedeficiency", + "questionnaireAnswers.FA1_preexistingconditions_heartdisease", + "questionnaireAnswers.FA1_preexistingconditions_asthmarequiringmedication", # nolint + "questionnaireAnswers.FA1_preexistingconditions_chroniclungdiseasenonasthma", # nolint + "questionnaireAnswers.FA1_preexistingconditions_chronicliverdisease", + "questionnaireAnswers.FA1_preexistingconditions_chronichaematologicaldisorder", # nolint + "questionnaireAnswers.FA1_preexistingconditions_chronickidneydisease", + "questionnaireAnswers.FA1_preexistingconditions_chronicneurological", + "questionnaireAnswers.FA1_preexistingconditions_organorbonemarrowrecipient", # nolint + "questionnaireAnswers.FA1_preexistingconditions_otherpreexistingcondition", # nolint + "questionnaireAnswers.FA1_healthcareinteractions_contactemergencynumber", + "questionnaireAnswers.FA1_priorXdayexposure_travelleddomestically", + "questionnaireAnswers.FA1_priorXdayexposure_travelledinternationally", + "questionnaireAnswers.FA1_priorXdayexposure_contactwithcase", + "questionnaireAnswers.FA1_priorXdayexposure_massgathering", + "questionnaireAnswers.FA1_priorXdayexposure_exposedtosimilarillness", + "questionnaireAnswers.FA1_priorXdayexposure_locationexposure", + "questionnaireAnswers.FA1_priorXdayexposure_inpatient", + "questionnaireAnswers.FA1_priorXdayexposure_outpatient", + "questionnaireAnswers.FA1_priorXdayexposure_traditionalhealer", + "questionnaireAnswers.FA1_formcompleted", "age.years", "age.months", + "duplicateKeys.name", "duplicateKeys.document", + "followUp.originalStartDate", "followUp.startDate", "followUp.endDate", + "followUp.status") + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "logical", "logical", "character", "character", + "character", "logical", "list", "character", "character", "character", + "character", "character", "character", "list", "list", "character", + "logical", "character", "character", "list", "list", "logical", "list", + "integer", "integer", "character", "character", "character", "character", + "character", "character", "character", "logical", "character", "logical", + "logical", "list", "logical", "character", "character", "character", + "character", "character", "character", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "integer", "integer", "list", + "list", "character", "character", "character", "character") + ) +}) diff --git a/tests/testthat/test-cases_from_contacts.R b/tests/testthat/test-cases_from_contacts.R new file mode 100644 index 0000000..cd09e3e --- /dev/null +++ b/tests/testthat/test-cases_from_contacts.R @@ -0,0 +1,64 @@ +test_that("cases_from_contacts works as expected", { + skip("get_cases requires API call") + + cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + locations <- get_locations( + url = url, + username = username, + password = password + ) + + locations_clean <- clean_locations(locations = locations) + + cases_vacc_history_clean <- clean_case_vax_history(cases = cases) + cases_address_history_clean <- clean_case_address_history( + cases = cases, + locations_clean = locations_clean + ) + cases_dateranges_history_clean <- clean_case_med_history(cases = cases) + + cases_clean <- clean_cases( + cases = cases, + cases_address_history_clean = cases_address_history_clean, + cases_vacc_history_clean = cases_vacc_history_clean, + cases_dateranges_history_clean = cases_dateranges_history_clean + ) + + res <- cases_from_contacts(cases_clean = cases_clean) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(3L, 43L)) + expect_true( + all(c( + "id", "visual_id", "classification", "follow_up_status", "first_name", + "middle_name", "last_name", "gender", "age", "age_class", "occupation", + "pregnancy_status", "date_of_reporting", "date_of_last_contact", + "date_of_burial", "risk_level", "risk_reason", "responsible_user_id", + "follow_up_team_id", "admin_0_name", "admin_1_name", "admin_2_name", + "lat", "long", "address", "postal_code", "city", "telephone", "email", + "vaccinated", "outcome", "date_of_outcome", "relationship_exposure_type", + "relationship_context_of_transmission", "relationship_exposure_duration", + "relationship_exposure_frequency", "relationship_certainty_level", + "relationship_cluster_id", "location_id", "created_by", + "datetime_created_at", "updated_by", "datetime_updated_at" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], function(x) class(x)[1])), + c( + rep("character", 8), "numeric", "factor", "character", "character", + "Date", "logical", "Date", "character", "character", "character", + "logical", "character", "character", "character", "numeric", "numeric", + rep("character", 5), "logical", "character", "Date", rep("logical", 6), + "character", "character", "POSIXct", "character", "POSIXct" + ) + ) +}) diff --git a/tests/testthat/test-check_godata_url.R b/tests/testthat/test-check_godata_url.R new file mode 100644 index 0000000..0022bf4 --- /dev/null +++ b/tests/testthat/test-check_godata_url.R @@ -0,0 +1,17 @@ +test_that("check_godata_url works as expected", { + skip("check_godata_url requires API call") + + res <- check_godata_url(url = url) + + expect_type(res, "logical") + expect_length(res, 1) +}) + +test_that("check_godata_url works as expected specifying success code", { + skip("check_godata_url requires API call") + + res <- check_godata_url(url = url, success_code = 200) + + expect_type(res, "logical") + expect_length(res, 1) +}) diff --git a/tests/testthat/test-check_godata_version.R b/tests/testthat/test-check_godata_version.R new file mode 100644 index 0000000..823650a --- /dev/null +++ b/tests/testthat/test-check_godata_version.R @@ -0,0 +1,5 @@ +test_that("check_godata_version works as expected", { + skip("check_godata_version requires API call") + + expect_true(check_godata_version(url = url)) +}) diff --git a/tests/testthat/test-clean_case_address_history.R b/tests/testthat/test-clean_case_address_history.R new file mode 100644 index 0000000..97a1363 --- /dev/null +++ b/tests/testthat/test-clean_case_address_history.R @@ -0,0 +1,50 @@ +test_that("clean_case_address_history works as expected", { + skip("get_cases requires API call") + + cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + locations <- get_locations( + url = url, + username = username, + password = password + ) + locations_clean <- clean_locations(locations = locations) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_case_address_history( + cases = cases, + locations_clean = locations_clean, + language_tokens = language_tokens + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(16L, 14L)) + expect_true( + all(c( + "id", "visualid", "addresses_locationid", "addresses_typeid", "lat", + "long", "address", "postal_code", "city", "telephone", "email", + "admin_0_name", "admin_1_name", "admin_2_name" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c( + "character", "character", "character", "character", "numeric", "numeric", + "character", "character", "character", "character", "character", + "character", "character", "character" + ) + ) +}) diff --git a/tests/testthat/test-clean_case_med_history.R b/tests/testthat/test-clean_case_med_history.R new file mode 100644 index 0000000..55cd50d --- /dev/null +++ b/tests/testthat/test-clean_case_med_history.R @@ -0,0 +1,41 @@ +test_that("clean_case_med_history works as expected", { + skip("get_cases requires API call") + + cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_case_med_history( + cases = cases, + language_tokens = language_tokens + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(7L, 9L)) + expect_true( + all(c( + "id", "visualid", "dateranges_typeid", "dateranges_centername", + "dateranges_locationid", "dateranges_startdate", "dateranges_enddate", + "dateranges_comments", "dateranges_id" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c( + "character", "character", "character", "character", "character", "Date", + "Date", "logical", "logical" + ) + ) +}) diff --git a/tests/testthat/test-clean_case_vax_history.R b/tests/testthat/test-clean_case_vax_history.R new file mode 100644 index 0000000..6372f45 --- /dev/null +++ b/tests/testthat/test-clean_case_vax_history.R @@ -0,0 +1,37 @@ +test_that("clean_case_vax_history works as expected", { + skip("get_cases requires API call") + + cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_case_vax_history( + cases = cases, + language_tokens = language_tokens + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(1L, 5L)) + expect_true( + all(c( + "id", "visualid", "vaccinesreceived_vaccine", "vaccinesreceived_status", + "vaccinesreceived_date" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "character", "Date") + ) +}) diff --git a/tests/testthat/test-clean_cases.R b/tests/testthat/test-clean_cases.R new file mode 100644 index 0000000..8a7445d --- /dev/null +++ b/tests/testthat/test-clean_cases.R @@ -0,0 +1,77 @@ +test_that("clean_cases works as expected", { + skip("get_cases requires API call") + + cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + locations <- get_locations( + url = url, + username = username, + password = password + ) + + locations_clean <- clean_locations(locations = locations) + + cases_vacc_history_clean <- clean_case_vax_history( + cases = cases, + language_tokens = language_tokens + ) + cases_address_history_clean <- clean_case_address_history( + cases = cases, + locations_clean = locations_clean, + language_tokens = language_tokens + ) + cases_dateranges_history_clean <- clean_case_med_history( + cases = cases, + language_tokens = language_tokens + ) + + res <- clean_cases( + cases = cases, + cases_address_history_clean = cases_address_history_clean, + cases_vacc_history_clean = cases_vacc_history_clean, + cases_dateranges_history_clean = cases_dateranges_history_clean, + language_tokens = language_tokens + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(14L, 43L)) + expect_true( + all(c( + "id", "visual_id", "classification", "first_name", "middle_name", + "last_name", "gender", "age", "age_class", "occupation", + "pregnancy_status", "date_of_reporting", "date_of_onset", + "date_of_infection", "date_become_case", "date_of_burial", "was_contact", + "risk_level", "risk_reason", "safe_burial", "transfer_refused", + "responsible_user_id", "admin_0_name", "admin_1_name", "admin_2_name", + "lat", "long", "address", "postal_code", "city", "telephone", "email", + "vaccinated", "isolated", "hospitalized", "icu", "outcome", + "date_of_outcome", "location_id", "created_by", "datetime_created_at", + "updated_by", "datetime_updated_at" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], function(x) class(x)[1])), + c( + rep("character", 7), "numeric", "factor", "character", "character", + rep("Date", 5), "logical", "character", "character", "logical", "logical", + "character", "character", "character", "character", "numeric", "numeric", + "character", "character", "character", "character", "character", + "logical", "logical", "logical", "logical", "character", "Date", + "character", "character", "POSIXct", "character", "POSIXct" + ) + ) +}) diff --git a/tests/testthat/test-clean_contact_address_history.R b/tests/testthat/test-clean_contact_address_history.R new file mode 100644 index 0000000..d5786f0 --- /dev/null +++ b/tests/testthat/test-clean_contact_address_history.R @@ -0,0 +1,50 @@ +test_that("clean_contact_address_history works as expected", { + skip("get_contacts requires API call") + + contacts <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + locations <- get_locations( + url = url, + username = username, + password = password + ) + locations_clean <- clean_locations(locations = locations) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_contact_address_history( + contacts = contacts, + locations_clean = locations_clean, + language_tokens = language_tokens + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(16L, 13L)) + expect_true( + all(c( + "id", "addresses_locationid", "addresses_typeid", "lat", "long", + "address", "postal_code", "city", "telephone", "email", "admin_0_name", + "admin_1_name", "admin_2_name" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c( + "character", "character", "character", "numeric", "numeric", + "character", "logical", "character", "character", "character", + "character", "character", "character" + ) + ) +}) diff --git a/tests/testthat/test-clean_contact_vax_history.R b/tests/testthat/test-clean_contact_vax_history.R new file mode 100644 index 0000000..a632cbf --- /dev/null +++ b/tests/testthat/test-clean_contact_vax_history.R @@ -0,0 +1,37 @@ +test_that("clean_contact_vax_history works as expected", { + skip("get_contacts requires API call") + + contacts <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_contact_vax_history( + contacts = contacts, + language_tokens = language_tokens + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(1L, 5L)) + expect_true( + all(c( + "id", "visualid", "vaccinesreceived_vaccine", "vaccinesreceived_date", + "vaccinesreceived_status" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "Date", "character") + ) +}) diff --git a/tests/testthat/test-clean_contacts.R b/tests/testthat/test-clean_contacts.R new file mode 100644 index 0000000..d3e1d5f --- /dev/null +++ b/tests/testthat/test-clean_contacts.R @@ -0,0 +1,104 @@ +test_that("clean_contacts works as expected", { + skip("get_contacts requires API call") + + contacts <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + locations <- get_locations( + url = url, + username = username, + password = password + ) + + locations_clean <- clean_locations(locations = locations) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + # other cleaned data required for `clean_contacts()` + contacts_vacc_history_clean <- clean_contact_vax_history( + contacts = contacts, + language_tokens = language_tokens + ) + contacts_address_history_clean <- clean_contact_address_history( + contacts = contacts, + locations_clean = locations_clean, + language_tokens = language_tokens + ) + + cases <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + cases_address_history_clean <- clean_case_address_history( + cases = cases, + locations_clean = locations_clean, + language_tokens = language_tokens + ) + cases_vacc_history_clean <- clean_case_vax_history( + cases = cases, + language_tokens = language_tokens + ) + cases_dateranges_history_clean <- clean_case_med_history( + cases = cases, + language_tokens = language_tokens + ) + + cases_clean <- clean_cases( + cases = cases, + cases_address_history_clean = cases_address_history_clean, + cases_vacc_history_clean = cases_vacc_history_clean, + cases_dateranges_history_clean = cases_dateranges_history_clean, + language_tokens = language_tokens + ) + contacts_becoming_cases <- cases_from_contacts(cases_clean = cases_clean) + + res <- clean_contacts( + contacts = contacts, + contacts_address_history_clean = cases_address_history_clean, + contacts_vacc_history_clean = cases_vacc_history_clean, + contacts_becoming_cases = contacts_becoming_cases + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(19L, 48L)) + expect_true( + all(c( + "id", "visual_id", "classification", "follow_up_status", "first_name", + "middle_name", "last_name", "gender", "age", "age_class", "occupation", + "pregnancy_status", "date_of_reporting", "date_of_last_contact", + "date_of_burial", "date_of_follow_up_start", "date_of_follow_up_end", + "was_case", "risk_level", "risk_reason", "safe_burial", + "transfer_refused", "responsible_user_id", "follow_up_team_id", + "admin_0_name", "admin_1_name", "admin_2_name", "lat", "long", "address", + "postal_code", "city", "telephone", "email", "vaccinated", "outcome", + "date_of_outcome", "relationship_exposure_type", + "relationship_context_of_transmission", "relationship_exposure_duration", + "relationship_exposure_frequency", "relationship_certainty_level", + "relationship_cluster_id", "location_id", "created_by", + "datetime_created_at", "updated_by", "datetime_updated_at" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], function(x) class(x)[1])), + c( + rep("character", 8), "numeric", "factor", "character", "character", + rep("Date", 5), "logical", "character", "character", "logical", "logical", + rep("character", 5), "numeric", "numeric", rep("character", 5), "logical", + "character", "Date", rep("character", 5), "logical", "character", + "character", "POSIXct", "character", "POSIXct" + ) + ) +}) diff --git a/tests/testthat/test-clean_contacts_of_contacts.R b/tests/testthat/test-clean_contacts_of_contacts.R new file mode 100644 index 0000000..740bfc9 --- /dev/null +++ b/tests/testthat/test-clean_contacts_of_contacts.R @@ -0,0 +1,62 @@ +test_that("clean_contacts_of_contacts works as expected", { + skip("get_contacts_of_contacts requires API call") + + contacts_of_contacts <- get_contacts_of_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + locations <- get_locations( + url = url, + username = username, + password = password + ) + locations_clean <- clean_locations(locations = locations) + + contacts_of_contacts_address_history_clean <- clean_contacts_of_contacts_address_history( + contacts_of_contacts = contacts_of_contacts, + locations_clean = locations_clean + ) + + contacts_of_contacts_vacc_history_clean <- clean_contacts_of_contacts_vax_history( + contacts_of_contacts = contacts_of_contacts + ) + + res <- clean_contacts_of_contacts( + contacts_of_contacts = contacts_of_contacts, + contacts_of_contacts_address_history_clean = contacts_of_contacts_address_history_clean, + contacts_of_contacts_vacc_history_clean = contacts_of_contacts_vacc_history_clean + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(14L, 43L)) + expect_true( + all(c( + "id", "visual_id", "classification", "first_name", "middle_name", + "last_name", "gender", "age", "age_class", "occupation", + "pregnancy_status", "date_of_reporting", "date_of_onset", + "date_of_infection", "date_become_case", "date_of_burial", "was_contact", + "risk_level", "risk_reason", "safe_burial", "transfer_refused", + "responsible_user_id", "admin_0_name", "admin_1_name", "admin_2_name", + "lat", "long", "address", "postal_code", "city", "telephone", "email", + "vaccinated", "isolated", "hospitalized", "icu", "outcome", + "date_of_outcome", "location_id", "created_by", "datetime_created_at", + "updated_by", "datetime_updated_at" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], function(x) class(x)[1])), + c( + rep("character", 7), "numeric", "factor", "character", "character", + rep("Date", 5), "logical", "character", "character", "logical", "logical", + "character", "character", "character", "character", "numeric", "numeric", + "character", "character", "character", "character", "character", + "logical", "logical", "logical", "logical", "character", "Date", + "character", "character", "POSIXct", "character", "POSIXct" + ) + ) +}) diff --git a/tests/testthat/test-clean_contacts_of_contacts_address_history.R b/tests/testthat/test-clean_contacts_of_contacts_address_history.R new file mode 100644 index 0000000..0314c4e --- /dev/null +++ b/tests/testthat/test-clean_contacts_of_contacts_address_history.R @@ -0,0 +1,49 @@ +test_that("clean_contacts_of_contacts_address_history works as expected", { + skip("get_contacts_of_contacts requires API call") + + contacts_of_contacts <- get_contacts_of_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + locations <- get_locations( + url = url, + username = username, + password = password + ) + locations_clean <- clean_locations(locations = locations) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_contacts_of_contacts_address_history( + contacts_of_contacts = contacts_of_contacts, + locations_clean = locations_clean, + language_tokens = language_tokens + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(1L, 13L)) + expect_true( + all(c( + "id", "addresses_locationid", "addresses_typeid", "lat", "long", + "address", "postal_code", "city", "telephone", "email", "admin_0_name", + "admin_1_name", "admin_2_name" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c( + "character", "character", "character", "numeric", "numeric", + rep("logical", 5), "character", "character", "character" + ) + ) +}) diff --git a/tests/testthat/test-clean_contacts_of_contacts_vax_history.R b/tests/testthat/test-clean_contacts_of_contacts_vax_history.R new file mode 100644 index 0000000..425c116 --- /dev/null +++ b/tests/testthat/test-clean_contacts_of_contacts_vax_history.R @@ -0,0 +1,37 @@ +test_that("clean_contacts_of_contacts_vax_history works as expected", { + skip("get_contacts_of_contacts requires API call") + + contacts_of_contacts <- get_contacts_of_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_contacts_of_contacts_vax_history( + contacts_of_contacts = contacts_of_contacts, + language_tokens = language_tokens + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(1L, 5L)) + expect_true( + all(c( + "id", "visualid", "vaccinesreceived_vaccine", "vaccinesreceived_date", + "vaccinesreceived_status" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "Date", "character") + ) +}) diff --git a/tests/testthat/test-clean_events.R b/tests/testthat/test-clean_events.R new file mode 100644 index 0000000..96b71e4 --- /dev/null +++ b/tests/testthat/test-clean_events.R @@ -0,0 +1,45 @@ +test_that("clean_events works as expected", { + skip("get_events requires API call") + + events <- get_events( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + locations <- get_locations( + url = url, + username = username, + password = password + ) + locations_clean <- clean_locations(locations = locations) + + res <- clean_events( + events = events, + locations_clean = locations_clean + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(3L, 21L)) + expect_true( + all(c( + "id", "name", "date", "date_of_reporting", "description", + "responsible_user", "admin_0_name", "admin_1_name", "admin_2_name", "lat", + "long", "address", "postal_code", "city", "telephone", "email", + "location_id", "created_by", "datetime_created_at", "updated_by", + "datetime_updated_at" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], function(x) class(x)[1])), + c( + "character", "character", "character", "Date", "logical", "logical", + "character", "character", "character", "numeric", "numeric", "logical", + "logical", "logical", "logical", "logical", "character", "character", + "POSIXct", "character", "POSIXct" + ) + ) +}) diff --git a/tests/testthat/test-clean_followups.R b/tests/testthat/test-clean_followups.R new file mode 100644 index 0000000..85724d5 --- /dev/null +++ b/tests/testthat/test-clean_followups.R @@ -0,0 +1,67 @@ +test_that("clean_followups works as expected", { + skip("get_followups requires API call") + + followups <- get_followups( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + contacts <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + locations <- get_locations( + url = url, + username = username, + password = password + ) + + locations_clean <- clean_locations(locations = locations) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + contacts_address_history_clean <- clean_contact_address_history( + contacts = contacts, + locations_clean = locations_clean, + language_tokens = language_tokens + ) + + res <- clean_followups( + followups = followups, + contacts_address_history_clean = contacts_address_history_clean + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(172L, 24L)) + expect_true( + all(c( + "id", "contact_id", "contact_visual_id", "date", "followup_number", + "followup_status", "targeted", "responsible_user_id", "team_id", + "admin_0_name", "admin_1_name", "admin_2_name", "lat", "long", "address", + "postal_code", "city", "telephone", "email", "location_id", "created_by", + "datetime_created_at", "updated_by", "datetime_updated_at" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], function(x) class(x)[1])), + c( + "character", "character", "character", "Date", "integer", "character", + "logical", "character", "character", "character", "character", + "character", "numeric", "numeric", "character", "logical", "character", + "character", "character", "character", "character", "POSIXct", + "character", "POSIXct" + ) + ) +}) diff --git a/tests/testthat/test-clean_locations.R b/tests/testthat/test-clean_locations.R new file mode 100644 index 0000000..95b6824 --- /dev/null +++ b/tests/testthat/test-clean_locations.R @@ -0,0 +1,43 @@ +test_that("clean_locations works as expected", { + skip("get_locations requires API call") + + locations <- get_locations( + url = url, + username = username, + password = password + ) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_locations(locations = locations, language_tokens = language_tokens) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(11L, 24L)) + expect_true( + all(c( + "location_id", "admin_level", "name", "parent_location_id", "lat", + "long", "admin_0_location_id", "admin_0_admin_level", "admin_0_name", + "admin_0_parent_location_id", "admin_0_lat", "admin_0_long", + "admin_1_location_id", "admin_1_admin_level", "admin_1_name", + "admin_1_parent_location_id", "admin_1_lat", "admin_1_long", + "admin_2_location_id", "admin_2_admin_level", "admin_2_name", + "admin_2_parent_location_id", "admin_2_lat", "admin_2_long" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c( + "character", "character", "character", "character", "numeric", "numeric", + "character", "character", "character", "character", "numeric", "numeric", + "character", "character", "character", "character", "numeric", "numeric", + "character", "character", "character", "character", "numeric", "numeric" + ) + ) +}) diff --git a/tests/testthat/test-clean_relationships.R b/tests/testthat/test-clean_relationships.R new file mode 100644 index 0000000..3fcac36 --- /dev/null +++ b/tests/testthat/test-clean_relationships.R @@ -0,0 +1,41 @@ +test_that("clean_relationships works as expected", { + skip("get_relationships requires API call") + + relationships <- get_relationships( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_relationships( + relationships = relationships, + language_tokens = language_tokens + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(29L, 11L)) + expect_true( + all(c( + "id", "source_person_id", "source_person_visual_id", "target_person_id", + "target_person_visual_id", "source_person_type", "target_person_type", + "created_by", "datetime_created_at", "updated_by", "datetime_updated_at" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], function(x) class(x)[1])), + c( + "character", "character", "character", "character", "character", + "character", "character", "character", "POSIXct", "character", "POSIXct" + ) + ) +}) diff --git a/tests/testthat/test-clean_teams.R b/tests/testthat/test-clean_teams.R new file mode 100644 index 0000000..5ca4029 --- /dev/null +++ b/tests/testthat/test-clean_teams.R @@ -0,0 +1,23 @@ +test_that("clean_teams works as expected", { + skip("get_teams requires API call") + + teams <- get_teams(url = url, username = username, password = password) + res <- clean_teams(teams = teams) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(5L, 15L)) + expect_true( + all(c( + "id", "name", "user_ids_1", "user_ids_2", "user_ids_3", "user_ids_4", + "user_ids_5", "location_ids_1", "location_ids_2", "location_ids_3", + "location_ids_4", "created_by", "datetime_created_at", "updated_by", + "datetime_updated_at" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + rep("character", 15) + ) +}) diff --git a/tests/testthat/test-clean_users.R b/tests/testthat/test-clean_users.R new file mode 100644 index 0000000..338b16c --- /dev/null +++ b/tests/testthat/test-clean_users.R @@ -0,0 +1,31 @@ +test_that("clean_users works as expected", { + skip("get_users requires API call") + + users <- get_users(url = url, username = username, password = password) + + language_tokens <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + res <- clean_users(users = users, language_tokens = language_tokens) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(20L, 24L)) + expect_true( + all(c( + "id", "first_name", "last_name", "email", "institution_name", + "disregard_geographic_restrictions", paste0("role_ids_", 1:14), + "active_outbreak_id", "created_by", "datetime_created_at", + "datetime_last_login" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c(rep("character", 5), "logical", rep("character", 18)) + ) +}) diff --git a/tests/testthat/test-contacts_per_case.R b/tests/testthat/test-contacts_per_case.R new file mode 100644 index 0000000..e6fbd0a --- /dev/null +++ b/tests/testthat/test-contacts_per_case.R @@ -0,0 +1,28 @@ +test_that("contacts_per_case works as expected", { + skip("get_relationships requires API call") + + relationships <- get_relationships( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + clean_relationships <- clean_relationships(relationships) + + res <- contacts_per_case(clean_relationships) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(10L, 2L)) + expect_true( + all(c( + "source_person_id", "no_contacts" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "integer") + ) +}) diff --git a/tests/testthat/test-export_downloader.R b/tests/testthat/test-export_downloader.R new file mode 100644 index 0000000..48ad43c --- /dev/null +++ b/tests/testthat/test-export_downloader.R @@ -0,0 +1,103 @@ +test_that("export_downloader works as expected", { + skip("export_downloader requires API call") + + api_call_request <- paste0( + url, "api/outbreaks/", outbreak_id, "/cases/export" + ) + res <- export_downloader( + url = url, + username = username, + password = password, + api_call_request = api_call_request, + wait = 2, + file_type = "json" + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(14L, 363L)) + expect_true( + all(c( + "id", "visualId", "dateOfReporting", "isDateOfReportingApproximate", + "createdAt", "createdBy", "updatedAt", "updatedBy", "deleted", + "deletedAt", "createdOn", "firstName", "middleName", "lastName", "gender", + "occupation", "dob", "classification", "wasContact", "dateBecomeCase", + "wasCase", "dateOfInfection", "dateOfOnset", "riskLevel", "riskReason", + "outcomeId", "dateOfOutcome", "documents", "type", "dateRanges", + "transferRefused", "addresses", "safeBurial", "dateOfBurial", + "isDateOfOnsetApproximate", "numberOfExposures", "numberOfContacts", + "deathLocationId", "deathLocationId Identifiers", + "deathLocationId Location geographical level", + "deathLocationId Parent location", "burialLocationId", + "burialLocationId Identifiers", + "burialLocationId Location geographical level", + "burialLocationId Parent location", "burialPlaceName", + "investigationStatus", "dateInvestigationCompleted", "vaccinesReceived", + "pregnancyStatus", "age.years", "age.months", "responsibleUser.firstName", + "responsibleUser.lastName", "responsibleUser.id" + ) %in% colnames(res)) + ) + + expect_true( + all(grepl(pattern = "^questionnaireAnswers", x = colnames(res)[56:363])) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "logical", "character", + "character", "character", "character", "logical", "logical", "character", + "character", "character", "character", "character", "character", + "character", "character", "logical", "character", "logical", "character", + "character", "character", "character", "character", "character", "list", + "character", "list", "logical", "list", "logical", "logical", "logical", + "integer", "integer", "logical", "list", "list", "list", "logical", + "list", "list", "list", "logical", "character", "logical", "list", + "character", "integer", "integer", "character", "character", "character", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "list", "list", "list", "list", "list", + "list", "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "list", "list", "list", "list", "list", "list", + "list", "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "logical", "list", "list", "list", "list", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "list", "logical", "logical", "list", + "logical", "logical", "logical", "list", "logical", "list", "logical", + "list", "logical", "list", "logical", "list", "logical", "list", + "logical", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "logical", "list", + "logical", "list", "logical", "logical", "logical", "logical", "list", + "logical", "logical", "logical", "logical", "list", "logical", "list", + "logical", "list", "list", "logical", "list", "logical", "list", + "logical", "list", "logical", "list", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "list", "list", + "list") + ) +}) diff --git a/tests/testthat/test-exposures_per_case.R b/tests/testthat/test-exposures_per_case.R new file mode 100644 index 0000000..e47ab2b --- /dev/null +++ b/tests/testthat/test-exposures_per_case.R @@ -0,0 +1,28 @@ +test_that("exposures_per_case works as expected", { + skip("get_relationships requires API call") + + relationships <- get_relationships( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + clean_relationships <- clean_relationships(relationships) + + res <- exposures_per_case(clean_relationships) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(23L, 2L)) + expect_true( + all(c( + "target_person_id", "no_exposures" + ) %in% colnames(res)) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "integer") + ) +}) diff --git a/tests/testthat/test-get_access_token.R b/tests/testthat/test-get_access_token.R new file mode 100644 index 0000000..a3c8943 --- /dev/null +++ b/tests/testthat/test-get_access_token.R @@ -0,0 +1,17 @@ +test_that("get_access_token works as expected", { + skip("get_access_token requires API call") + + res <- get_access_token( + url = url, + username = username, + password = password + ) + + expect_type(res, "character") + # character string can contain alphanumeric characters + expect_true(grepl(pattern = "[:alphanum:]", x = res)) + # character string cannot contain punctuation marks + expect_false(grepl(pattern = "[[:punct:]]", x = res)) + # character string cannot contain spaces + expect_false(grepl(pattern = "\\s", x = res)) +}) diff --git a/tests/testthat/test-get_active_outbreak.R b/tests/testthat/test-get_active_outbreak.R new file mode 100644 index 0000000..d000c2f --- /dev/null +++ b/tests/testthat/test-get_active_outbreak.R @@ -0,0 +1,17 @@ +test_that("get_active_outbreak works as expected", { + skip("get_active_outbreak requires API call") + + res <- get_active_outbreak( + url = url, + username = username, + password = password + ) + + expect_type(res, "character") + # character string can contain alphanumeric characters + expect_true(grepl(pattern = "[:alphanum:]", x = res)) + # character string can contain punctuation marks + expect_true(grepl(pattern = "[:punct:]", x = res)) + # character string cannot contain spaces + expect_false(grepl(pattern = "\\s", x = res)) +}) diff --git a/tests/testthat/test-get_all_outbreaks.R b/tests/testthat/test-get_all_outbreaks.R new file mode 100644 index 0000000..4530c3f --- /dev/null +++ b/tests/testthat/test-get_all_outbreaks.R @@ -0,0 +1,21 @@ +test_that("get_all_outbreaks works as expected", { + skip("get_all_outbreaks requires API call") + + res <- get_all_outbreaks( + url = url, + username = username, + password = password + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(11L, 5L)) + expect_identical( + colnames(res), + c("id", "name", "description", "createdBy", "createdAt") + ) + expect_identical( + unname(sapply(res[1, ], class)), + rep("character", 5) + ) +}) diff --git a/tests/testthat/test-get_cases.R b/tests/testthat/test-get_cases.R new file mode 100644 index 0000000..504cb8d --- /dev/null +++ b/tests/testthat/test-get_cases.R @@ -0,0 +1,96 @@ +test_that("get_cases works as expected", { + skip("get_cases requires API call") + + res <- get_cases( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id, + method = "export", + batch_size = 50000, + wait = 2, + file_type = "json" + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(13L, 357L)) + expect_true( + all(c( + "id", "visualId", "dateOfReporting", "isDateOfReportingApproximate", + "createdAt", "createdBy", "updatedAt", "updatedBy", "deleted", + "deletedAt", "createdOn", "firstName", "middleName", "lastName", "gender", + "occupation", "dob", "classification", "wasContact", "dateBecomeCase", + "wasCase", "dateOfInfection", "dateOfOnset", "riskLevel", "riskReason", + "outcomeId", "dateOfOutcome", "documents", "type", "dateRanges", + "transferRefused", "addresses", "safeBurial", "dateOfBurial", + "isDateOfOnsetApproximate", "numberOfExposures", "numberOfContacts", + "burialLocationId", "burialLocationId Identifiers", + "burialLocationId Location geographical level", + "burialLocationId Parent location", "burialPlaceName", + "investigationStatus", "dateInvestigationCompleted", "vaccinesReceived", + "pregnancyStatus", "responsibleUserId", "age.years", "age.months" + ) %in% colnames(res)) + ) + + expect_true( + all(grepl(pattern = "^questionnaireAnswers", x = colnames(res)[50:357])) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "logical", "character", + "character", "character", "character", "logical", "logical", "character", + "character", "character", "character", "character", "character", + "character", "character", "logical", "character", "logical", "character", + "character", "character", "character", "character", "character", "list", + "character", "list", "logical", "list", "logical", "logical", "logical", + "integer", "integer", "logical", "list", "list", "list", "logical", + "character", "logical", "list", "character", "character", "integer", + "integer", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "list", "list", "list", + "list", "list", "list", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "list", "list", "list", + "list", "list", "list", "list", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "list", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "logical", + "list", "list", "list", "list", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "list", "logical", "logical", "list", "logical", "logical", + "logical", "list", "logical", "list", "logical", "list", "logical", + "list", "logical", "list", "logical", "list", "logical", "list", "list", + "list", "list", "list", "list", "list", "list", "list", "list", "list", + "list", "list", "list", "list", "logical", "list", "logical", "list", + "logical", "logical", "logical", "logical", "list", "logical", "logical", + "logical", "logical", "list", "logical", "list", "logical", "list", + "list", "logical", "list", "logical", "list", "logical", "list", + "logical", "list", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "logical", "list", "list", "list")) +}) diff --git a/tests/testthat/test-get_clusters.R b/tests/testthat/test-get_clusters.R new file mode 100644 index 0000000..3155a80 --- /dev/null +++ b/tests/testthat/test-get_clusters.R @@ -0,0 +1,23 @@ +test_that("get_clusters works as expected", { + skip("get_clusters requires API call") + + res <- get_clusters( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(1L, 11L)) + expect_identical( + colnames(res), + c("name", "description", "icon", "colorCode", "id", "createdAt", + "createdBy", "updatedAt", "updatedBy", "createdOn", "deleted") + ) + expect_identical( + unname(sapply(res[1, ], class)), + c(rep("character", 10), "logical") + ) +}) diff --git a/tests/testthat/test-get_contacts.R b/tests/testthat/test-get_contacts.R new file mode 100644 index 0000000..0df14a6 --- /dev/null +++ b/tests/testthat/test-get_contacts.R @@ -0,0 +1,55 @@ +test_that("get_contacts works as expected", { + skip("get_contacts requires API call") + + res <- get_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(14L, 64L)) + expect_identical( + colnames(res), + c("id", "visualId", "dateOfReporting", "isDateOfReportingApproximate", + "createdAt", "createdBy", "updatedAt", "updatedBy", "deleted", + "deletedAt", "createdOn", "firstName", "middleName", "lastName", "gender", + "occupation", "dob", "classification", "wasContact", "wasCase", + "dateBecomeContact", "riskLevel", "riskReason", "outcomeId", + "dateOfOutcome", "documents", "type", "transferRefused", "addresses", + "safeBurial", "dateOfBurial", "followUpTeamId", "dateOfLastContact", + "numberOfExposures", "numberOfContacts", "vaccinesReceived", + "pregnancyStatus", "responsibleUserId", "age.years", "age.months", + "followUp.originalStartDate", "followUp.startDate", "followUp.endDate", + "followUp.status", "relationship.contactDate", + "relationship.contactDateEstimated", "relationship.certaintyLevelId", + "relationship.createdAt", "relationship.createdBy", + "relationship.updatedAt", "relationship.updatedBy", + "relationship.createdOn", "relationship.deleted", + "relationship.relatedId", "relationship.id", + "relationship.exposureTypeId", "relationship.exposureFrequencyId", + "relationship.exposureDurationId", + "relationship.socialRelationshipTypeId", + "relationship.socialRelationshipDetail", "relationship.clusterId", + "relationship.comment", "relationship.deletedAt", + "questionnaireAnswers.test") + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "logical", "character", + "character", "character", "character", "logical", "logical", "character", + "character", "logical", "character", "character", "character", "logical", + "character", "logical", "logical", "character", "character", "character", + "logical", "logical", "list", "character", "logical", "list", "logical", + "logical", "character", "character", "integer", "integer", "list", + "logical", "character", "integer", "integer", "character", "character", + "character", "character", "character", "logical", "character", + "character", "character", "character", "character", "character", + "logical", "character", "character", "character", "character", + "character", "character", "character", "logical", "logical", "logical", + "list") + ) +}) diff --git a/tests/testthat/test-get_contacts_of_contacts.R b/tests/testthat/test-get_contacts_of_contacts.R new file mode 100644 index 0000000..170436f --- /dev/null +++ b/tests/testthat/test-get_contacts_of_contacts.R @@ -0,0 +1,37 @@ +test_that("get_contacts_of_contacts works as expected", { + skip("get_contacts_of_contacts requires API call") + + res <- get_contacts_of_contacts( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(1L, 39L)) + expect_identical( + colnames(res), + c("id", "visualId", "dateOfReporting", "isDateOfReportingApproximate", + "createdAt", "createdBy", "updatedAt", "updatedBy", "deleted", + "deletedAt", "createdOn", "firstName", "middleName", "lastName", + "gender", "occupation", "dob", "classification", "wasContact", "wasCase", + "dateBecomeContact", "riskLevel", "riskReason", "outcomeId", + "dateOfOutcome", "documents", "type", "transferRefused", "addresses", + "safeBurial", "dateOfBurial", "dateOfLastContact", "numberOfExposures", + "vaccinesReceived", "pregnancyStatus", "responsibleUserId", + "relationship", "age.years", "age.months") + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "logical", "character", + "character", "character", "character", "logical", "logical", "character", + "character", "logical", "character", "character", "logical", "logical", + "logical", "logical", "logical", "logical", "logical", "logical", + "logical", "logical", "list", "character", "logical", "list", "logical", + "logical", "character", "integer", "list", "character", "logical", + "logical", "integer", "logical") + ) +}) diff --git a/tests/testthat/test-get_date_range.R b/tests/testthat/test-get_date_range.R new file mode 100644 index 0000000..41593d9 --- /dev/null +++ b/tests/testthat/test-get_date_range.R @@ -0,0 +1,77 @@ +test_that("get_date_range works as expected", { + x <- c("2020-07-15", "2021-08-09", "2022-08-03") + date_range <- get_date_range(dates = x) + + expect_type(date_range, "list") + expect_named(date_range, c("mindate", "maxdate")) + expect_identical( + date_range, + list( + mindate = as.POSIXct("2020-07-15", tz = "UTC"), + maxdate = as.POSIXct("2022-08-03", tz = "UTC") + ) + ) +}) + +test_that("get_date_range works as expected with missing values", { + x <- c("2020-07-15", NA, "2021-08-09", "2022-08-03", NA) + date_range <- get_date_range(dates = x) + + expect_type(date_range, "list") + expect_named(date_range, c("mindate", "maxdate")) + expect_identical( + date_range, + list( + mindate = as.POSIXct("2020-07-15", tz = "UTC"), + maxdate = as.POSIXct("2022-08-03", tz = "UTC") + ) + ) +}) + +test_that("get_date_range works as expected with different formats", { + # first 3 are ymd, last is dmy + x <- c("2020-07-15", "2021-08-09", "2022-08-03", "13-04-2023") + date_range <- get_date_range(dates = x) + + expect_type(date_range, "list") + expect_named(date_range, c("mindate", "maxdate")) + expect_identical( + date_range, + list( + mindate = as.POSIXct("2020-07-15", tz = "UTC"), + maxdate = as.POSIXct("2023-04-13", tz = "UTC") + ) + ) +}) + +test_that("get_date_range cannot handle ambiguous mixed formats", { + # last two are dmy and mdy, so max date should be the last one + x <- c("2020-07-15", "2021-08-09", "2022-08-03", "01-04-2023", "05-01-2023") + date_range <- get_date_range(dates = x) + + expect_type(date_range, "list") + expect_named(date_range, c("mindate", "maxdate")) + expect_identical( + date_range, + list( + mindate = as.POSIXct("2020-07-15", tz = "UTC"), + maxdate = as.POSIXct("2023-04-01", tz = "UTC") + ) + ) +}) + +test_that("get_date_range throws warning with incorrect dates", { + x <- c("2020-07-15", "2021-08-09", "2022-08-03", "2023-13-01") + expect_warning(date_range <- get_date_range(dates = x)) + + expect_type(date_range, "list") + expect_named(date_range, c("mindate", "maxdate")) + expect_identical( + date_range, + list( + mindate = as.POSIXct("2020-07-15", tz = "UTC"), + maxdate = as.POSIXct("2022-08-03", tz = "UTC") + ) + ) +}) + diff --git a/tests/testthat/test-get_events.R b/tests/testthat/test-get_events.R new file mode 100644 index 0000000..b66d8ee --- /dev/null +++ b/tests/testthat/test-get_events.R @@ -0,0 +1,43 @@ +test_that("get_events works as expected", { + skip("get_events requires API call") + + res <- get_events( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id, + method = "export", + batch_size = 50000, + wait = 2, + file_type = "json" + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(2L, 34L)) + expect_true( + all(c( + "id", "dateOfReporting", "isDateOfReportingApproximate", "createdAt", + "createdBy", "updatedAt", "updatedBy", "deleted", "deletedAt", + "createdOn", "type", "numberOfExposures", "numberOfContacts", "name", + "date", "description", "responsibleUserId", "eventCategory", "endDate", + "address.typeId", "address.locationId", "address.Identifiers", + "address.Location geographical level", "address.Parent location", + "address.geoLocationAccurate", "address.date", "address.country", + "address.city", "address.addressLine1", "address.postalCode", + "address.phoneNumber", "address.emailAddress", "address.geoLocation.lat", + "address.geoLocation.lng" + ) %in% colnames(res)) + ) + + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "logical", "character", "character", + "character", "character", "logical", "logical", "character", "character", + "integer", "integer", "character", "character", "logical", "logical", + "logical", "character", "character", "character", "list", "list", + "list", "logical", "character", "logical", "logical", "logical", + "logical", "logical", "logical", "numeric", "numeric")) + +}) diff --git a/tests/testthat/test-get_export_status.R b/tests/testthat/test-get_export_status.R new file mode 100644 index 0000000..e27c1db --- /dev/null +++ b/tests/testthat/test-get_export_status.R @@ -0,0 +1,42 @@ +test_that("get_export_status works as expected", { + skip("get_export_status requires API call") + + api_call_request <- paste0( + url, "api/outbreaks/", outbreak_id, "/cases/export" + ) + + export_log_id_request <- httr::GET( + paste0( + api_call_request, + "?filter=%7B%22where%22%3A%7B%22useDbColumns%22%3A%22true%22%2C%20%22", + "dontTranslateValues%22%3A%22true%22%2C%20%22", + "jsonReplaceUndefinedWithNull%22%3A%22true%22%20%7D%7D", + "&type=", + "json", + "&access_token=", + get_access_token( + url = url, + username = username, + password = password + ) + ) + ) + + export_log_id_request_content <- httr::content(export_log_id_request) + + request_id <- purrr::pluck(export_log_id_request_content, "exportLogId") + + res <- get_export_status( + url = url, + username = username, + password = password, + request_id = request_id + ) + + expect_type(res, "list") + expect_length(res, 3) + expect_named(res, c("statusStep", "totalNo", "processedNo")) + expect_type(res$statusStep, "character") + expect_type(res$totalNo, "integer") + expect_type(res$processedNo, "integer") +}) diff --git a/tests/testthat/test-get_followups.R b/tests/testthat/test-get_followups.R new file mode 100644 index 0000000..345df53 --- /dev/null +++ b/tests/testthat/test-get_followups.R @@ -0,0 +1,48 @@ +test_that("get_followups works as expected", { + skip("get_followups requires API call") + + res <- get_followups( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id, + method = "export", + batch_size = 50000, + wait = 2, + file_type = "json" + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(49L, 201L)) + expect_true( + all(c( + "id", "date", "index", "createdAt", "createdBy", "updatedAt", "updatedBy", + "deleted", "deletedAt", "createdOn", "fillLocation", "teamId", "statusId", + "targeted", "comment", "responsibleUserId", "contact.firstName", + "contact.lastName", "contact.visualId", "contact.id", "address.typeId", + "address.locationId", "address.Identifiers", + "address.Location geographical level", "address.Parent location", + "address.geoLocationAccurate", "address.date", "address.country", + "address.city", "address.addressLine1", "address.postalCode", + "address.phoneNumber", "address.emailAddress", "address.geoLocation.lat", + "address.geoLocation.lng" + ) %in% colnames(res)) + ) + + expect_true( + all(grepl(pattern = "^questionnaireAnswers", x = colnames(res)[36:201])) + ) + + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "integer", "character", "character", + "character", "character", "logical", "logical", "character", "logical", + "character", "character", "logical", "logical", "character", "character", + "character", "character", "character", "character", "character", "list", + "list", "list", "logical", "character", "logical", "logical", "logical", + "logical", "logical", "logical", "numeric", "numeric", "list", "list", + "list", "list", "list", "list", "list", "logical", "list", "logical", + "list", "list", rep("logical", 152), "list", "list")) +}) diff --git a/tests/testthat/test-get_godata_version.R b/tests/testthat/test-get_godata_version.R new file mode 100644 index 0000000..cc90c58 --- /dev/null +++ b/tests/testthat/test-get_godata_version.R @@ -0,0 +1,13 @@ +test_that("get_godata_versions works as expected", { + skip("get_godata_version requires API call") + + res <- get_godata_version(url = url) + + expect_type(res, "character") + # character string can contain digits + expect_true(grepl(pattern = "\\d", x = res)) + # character string can contain full stops + expect_true(grepl(pattern = ".", x = res)) + # character string cannot contain alphabetic characters + expect_false(grepl(pattern = "[:alpha:]", x = res)) +}) diff --git a/tests/testthat/test-get_labresults.R b/tests/testthat/test-get_labresults.R new file mode 100644 index 0000000..3f0b7c5 --- /dev/null +++ b/tests/testthat/test-get_labresults.R @@ -0,0 +1,58 @@ +test_that("get_labresults works as expected", { + skip("get_labresults requires API call") + + res <- get_labresults( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id, + method = "export", + batch_size = 50000, + wait = 2, + file_type = "json" + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(9L, 55L)) + expect_true( + all(c( + "id", "createdAt", "createdBy", "updatedAt", "updatedBy", "deleted", + "deletedAt", "createdOn", "personId", "dateSampleTaken", + "dateSampleDelivered", "dateTesting", "dateOfResult", "labName", + "sampleIdentifier", "sampleType", "testType", "testedFor", "result", + "quantitativeResult", "notes", "status", "sequence.hasSequence", + "sequence.dateSampleSent", "sequence.labId", "sequence.dateResult", + "sequence.resultId", "sequence.noSequenceReason", "person.visualId", + "person.type", "person.lastName", "person.firstName", + "person.dateOfOnset", "person.dateOfReporting", "person.middleName", + "person.address.typeId", "person.address.city", + "person.address.locationId", "person.address.Identifiers", + "person.address.Location geographical level", + "person.address.Parent location", "person.address.geoLocationAccurate", + "person.address.date", "person.address.country", + "person.address.addressLine1", "person.address.postalCode", + "person.address.phoneNumber", "person.address.emailAddress", + "person.address.geoLocation.lat", "person.address.geoLocation.lng", + "questionnaireAnswers.Lab_SpecimenCollection_Symptoms", + "questionnaireAnswers.Lab_SpecimenShippedAnotherLaboratory", + "questionnaireAnswers.test", + "questionnaireAnswers.Lab_specimenshipped_laboratoryname", + "questionnaireAnswers.Lab_specimenshipped_dateofshipping" + ) %in% colnames(res)) + ) + + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "character", "character", + "logical", "logical", "character", "character", "character", "character", + "character", "character", "logical", "character", "character", + "character", "logical", "character", "logical", "logical", "character", + "logical", "character", "character", "character", "character", "logical", + "character", "character", "character", "character", "character", + "character", "character", "character", "character", "character", "list", + "list", "list", "logical", "character", "logical", "logical", "logical", + "logical", "logical", "numeric", "numeric", "list", "list", "list", + "list", "list")) +}) diff --git a/tests/testthat/test-get_language_tokens.R b/tests/testthat/test-get_language_tokens.R new file mode 100644 index 0000000..4985c6e --- /dev/null +++ b/tests/testthat/test-get_language_tokens.R @@ -0,0 +1,24 @@ +test_that("get_language_tokens works as expected", { + skip("get_language_tokens requires API call") + + res <- get_language_tokens( + url = url, + username = username, + password = password, + language = "english_us" + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(13641L, 3L)) + expect_identical( + colnames(res), + c("languageId", "lastUpdateDate", "tokens") + ) + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "data.frame") + ) + expect_identical(colnames(res$tokens), c("token", "translation")) + expect_identical(dim(res$tokens), c(13641L, 2L)) +}) diff --git a/tests/testthat/test-get_languages.R b/tests/testthat/test-get_languages.R new file mode 100644 index 0000000..16cc933 --- /dev/null +++ b/tests/testthat/test-get_languages.R @@ -0,0 +1,23 @@ +test_that("get_languages works as expected", { + skip("get_languages requires API call") + + res <- get_languages( + url = url, + username = username, + password = password + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(6L, 9L)) + expect_identical( + colnames(res), + c("name", "readOnly", "id", "createdAt", "createdBy", "updatedAt", + "updatedBy", "createdOn", "deleted") + ) + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "logical", "character", "character", "character", + "character", "character", "character", "logical") + ) +}) diff --git a/tests/testthat/test-get_locations.R b/tests/testthat/test-get_locations.R new file mode 100644 index 0000000..fc610d0 --- /dev/null +++ b/tests/testthat/test-get_locations.R @@ -0,0 +1,24 @@ +test_that("get_locations works as expected", { + skip("get_locations requires API call") + + res <- get_locations(url = url, username = username, password = password) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(11L, 16L)) + expect_true( + all(c( + "name", "synonyms", "identifiers", "active", "populationDensity", + "parentLocationId", "geographicalLevelId", "id", "createdAt", + "createdBy", "updatedAt", "updatedBy", "createdOn", "deleted", + "geoLocation.lat", "geoLocation.lng" + ) %in% colnames(res)) + ) + + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "list", "list", "logical", "integer", "character", + "character", "character", "character", "character", "character", + "character", "character", "logical", "numeric", "numeric")) +}) diff --git a/tests/testthat/test-get_reference_data.R b/tests/testthat/test-get_reference_data.R new file mode 100644 index 0000000..864014b --- /dev/null +++ b/tests/testthat/test-get_reference_data.R @@ -0,0 +1,25 @@ +test_that("get_reference_data works as expected", { + skip("get_reference_data requires API call") + + res <- get_reference_data(url = url, username = username, password = password) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(391L, 19L)) + expect_true( + all(c( + "categoryId", "value", "description", "readOnly", "active", + "isDefaultReferenceData", "id", "createdAt", "createdBy", "updatedAt", + "updatedBy", "createdOn", "deleted", "colorCode", "order", + "isOutbreakTemplateReferenceData", "deletedAt", "iconId", "code" + ) %in% colnames(res)) + ) + + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "logical", "logical", "logical", + "character", "character", "character", "character", "character", + "character", "logical", "character", "integer", "logical", "logical", + "character", "character")) +}) diff --git a/tests/testthat/test-get_relationships.R b/tests/testthat/test-get_relationships.R new file mode 100644 index 0000000..3b534e3 --- /dev/null +++ b/tests/testthat/test-get_relationships.R @@ -0,0 +1,47 @@ +test_that("get_relationships works as expected", { + skip("get_relationships requires API call") + + res <- get_relationships( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id, + method = "export", + batch_size = 50000, + wait = 2, + file_type = "json" + ) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(29L, 43L)) + expect_true( + all(c( + "id", "createdAt", "createdBy", "updatedAt", "updatedBy", "deleted", + "deletedAt", "createdOn", "dateOfFirstContact", "contactDate", + "contactDateEstimated", "certaintyLevelId", "exposureTypeId", + "exposureFrequencyId", "exposureDurationId", "socialRelationshipTypeId", + "socialRelationshipDetail", "clusterId", "comment", "sourcePerson.type", + "sourcePerson.firstName", "sourcePerson.gender", "sourcePerson.visualId", + "sourcePerson.lastName", "sourcePerson.dob", "sourcePerson.id", + "sourcePerson.source", "sourcePerson.name", "sourcePerson.middleName", + "sourcePerson.age.years", "sourcePerson.age.months", "targetPerson.type", + "targetPerson.firstName", "targetPerson.gender", "targetPerson.visualId", + "targetPerson.lastName", "targetPerson.id", "targetPerson.target", + "targetPerson.name", "targetPerson.middleName", "targetPerson.dob", + "targetPerson.age.years", "targetPerson.age.months" + ) %in% colnames(res)) + ) + + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "character", "character", + "logical", "logical", "character", "character", "character", "logical", + "character", "character", "character", "character", "character", + "character", "character", "character", "character", "character", + "character", "character", "character", "logical", "character", "logical", + "logical", "character", "integer", "integer", "character", "character", + "character", "character", "character", "character", "logical", + "character", "character", "character", "integer", "integer")) +}) diff --git a/tests/testthat/test-get_teams.R b/tests/testthat/test-get_teams.R new file mode 100644 index 0000000..efc960b --- /dev/null +++ b/tests/testthat/test-get_teams.R @@ -0,0 +1,22 @@ +test_that("get_teams works as expected", { + skip("get_teams requires API call") + + + res <- get_teams(url = url, username = username, password = password) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(3L, 10L)) + expect_true( + all(c( + "name", "userIds", "locationIds", "id", "createdAt", "createdBy", + "updatedAt", "updatedBy", "createdOn", "deleted" + ) %in% colnames(res)) + ) + + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "list", "list", "character", "character", "character", + "character", "character", "character", "logical")) +}) diff --git a/tests/testthat/test-get_users.R b/tests/testthat/test-get_users.R new file mode 100644 index 0000000..d3d69ac --- /dev/null +++ b/tests/testthat/test-get_users.R @@ -0,0 +1,30 @@ +test_that("get_users works as expected", { + skip("get_users requires API call") + + res <- get_users(url = url, username = username, password = password) + + expect_s3_class(res, "tbl_df") + expect_s3_class(res, "data.frame") + expect_identical(dim(res), c(20L, 67L)) + expect_true( + all(c( + "id", "firstName", "lastName", "roleIds", "activeOutbreakId", + "languageId", "passwordChange", "loginRetriesCount", "lastLoginDate", + "disregardGeographicRestrictions", "dontCacheFilters", "email", + "createdAt", "createdBy", "updatedAt", "updatedBy", "deleted", + "createdOn", "securityQuestions", "outbreakIds", "institutionName" + ) %in% colnames(res)) + ) + + expect_true( + all(grepl(pattern = "^settings", x = colnames(res)[22:66])) + ) + + expect_identical( + unname(sapply(res[1, ], class)), + c("character", "character", "character", "list", "character", "character", + "logical", "integer", "character", "logical", "logical", "character", + "character", "character", "character", "character", "logical", + "character", "list", "list", "character", rep("list", 12), "logical", + rep("list", 29), "logical", "list", "list", "character")) +}) diff --git a/tests/testthat/test-mongify_date.R b/tests/testthat/test-mongify_date.R new file mode 100644 index 0000000..35b7ca4 --- /dev/null +++ b/tests/testthat/test-mongify_date.R @@ -0,0 +1,66 @@ +test_that("mongify_date works as expected", { + + dates <- c("08/01/2022", "08/31/2022") + converted_dates <- mongify_date(dates = dates, dateformat = "mdy") + + expect_type(converted_dates, "character") + expect_length(converted_dates, length(dates)) + expect_true( + all(grepl( + pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z", + x = converted_dates + )) + ) +}) + +test_that("mongify_date works as expected with undefined format", { + + dates <- c("08/01/2022", "08/31/2022") + converted_dates <- mongify_date(dates = dates, dateformat = "undefined") + + expect_type(converted_dates, "character") + expect_length(converted_dates, length(dates)) + expect_true( + all(grepl( + pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z", + x = converted_dates + )) + ) +}) + +test_that("mongify_date works as expected with ymd format", { + + dates <- c("2022/08/01", "2022/08/31") + converted_dates <- mongify_date(dates = dates, dateformat = "ymd") + + expect_type(converted_dates, "character") + expect_length(converted_dates, length(dates)) + expect_true( + all(grepl( + pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z", + x = converted_dates + )) + ) +}) + +test_that("mongify_date throws warning with incorrect format", { + + # second date cannot be dmy format + dates <- c("08/01/2022", "08/31/2022") + expect_warning( + converted_dates <- mongify_date(dates = dates, dateformat = "dmy"), + regexp = "1 failed to parse." + ) + + expect_type(converted_dates, "character") + expect_true(anyNA(converted_dates)) + expect_length(converted_dates, length(dates)) + # conforms to expected format or is NA + expect_true( + all(grepl( + pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z", + x = converted_dates + ) | is.na(converted_dates) + ) + ) +}) diff --git a/tests/testthat/test-set_active_outbreak.R b/tests/testthat/test-set_active_outbreak.R new file mode 100644 index 0000000..8a3ec4c --- /dev/null +++ b/tests/testthat/test-set_active_outbreak.R @@ -0,0 +1,32 @@ +test_that("set_active_outbreak works as expected", { + skip("set_active_outbreak requires API call") + + expect_message( + res <- set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = outbreak_id + ), + regexp = "Active outbreak not changed." + ) + + expect_type(res, "character") +}) + +test_that("set_active_outbreak works as expected with non-valid outbreak id", { + skip("set_active_outbreak requires API call") + + expect_error( + set_active_outbreak( + url = url, + username = username, + password = password, + outbreak_id = "123" + ), + regexp = paste0( + "Active outbreak not changed. 123 not in list of user's ", + "available outbreaks" + ) + ) +})