Skip to content

Commit

Permalink
additional error testing
Browse files Browse the repository at this point in the history
  • Loading branch information
evanodell committed Jun 25, 2018
1 parent 47b880c commit ff994b3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 56 deletions.
52 changes: 22 additions & 30 deletions R/data_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,21 @@
#'
#' @param select A character vector of one or more variables to select,
#' excluding all others. \code{select} is not case sensitive.
#'
#' @param tidy Logical parameter. If \code{TRUE}, converts variable names to
#'
#' @param tidy Logical parameter. If \code{TRUE}, converts variable names to
#' \code{snake_case}. Defaults to \code{TRUE}.
#'
#'
#' @param tidy_style The style to convert variable names to, if
#' \code{tidy = TRUE}. Accepts one of \code{'snake_case'}, \code{'camelCase'}
#' and \code{'period.case'}. Defaults to \code{'snake_case'}.
#' \code{tidy = TRUE}. Accepts one of \code{"snake_case"}, \code{"camelCase"}
#' and \code{"period.case"}. Defaults to \code{"snake_case"}.
#'
#' @param ... Use to pass any other parameters to the API. Useful for passing
#' concepts that are not available through the default parameters. Only accepts
#' concepts identified in \code{\link{nomis_get_metadata}} and concept values
#' identified in \code{\link{nomis_codelist}}. Parameters can be quoted or
#' unquoted. Each parameter should have a name and a value. For example,
#' \code{CAUSE_OF_DEATH = 10300} when querying dataset \code{"NM_161_1"}. Some
#' parameters are case sensitive and some are not, it is unclear why this is
#' the case. It is reccomended
#' unquoted. Each parameter should have a name and a value. For example,
#' \code{CAUSE_OF_DEATH = 10300} when querying dataset \code{"NM_161_1"}.
#' Parameters are not case sensitive.
#'
#' @return A tibble containing the selected dataset.
#' By default, all tibble columns are parsed as characters.
Expand Down Expand Up @@ -172,12 +171,12 @@
nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
sex = NULL, measures = NULL,
additional_queries = NULL, exclude_missing = FALSE,
select = NULL, tidy = TRUE,
select = NULL, tidy = TRUE,
tidy_style = "snake_case", ...) {
if (missing(id)) {
stop("Dataset ID must be specified", call. = FALSE)
}

# check for use or time or data parameter
if (is.null(date) == FALSE) {
time_query <- paste0("&date=", paste0(date, collapse = ","))
Expand Down Expand Up @@ -244,22 +243,22 @@ nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
),
""
)

dots <- rlang::list2(...) ## eval the dots
names(dots) <- toupper(names(dots))
x <- c()


for (i in seq_along(dots)) { # retrieve the dots
x[i] <- ifelse(length(dots[[i]]) > 0,
paste0(
"&", toupper(names(dots[i])), "=",
paste0(dots[[i]], collapse = ",")
),
""
paste0(
"&", toupper(names(dots[i])), "=",
paste0(dots[[i]], collapse = ",")
),
""
)
}

dots_query <- paste0(x, collapse = "")

if (!is.null(getOption("nomisr.API.key"))) {
Expand All @@ -271,20 +270,14 @@ nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
}

query <- paste0(
id, ".data.csv?",dots_query, time_query, geography_query, sex_query,
exclude_query, select_query, api_query,
id, ".data.csv?", dots_query, time_query, geography_query, sex_query,
exclude_query, select_query, api_query,
additional_query, measures_query

)

first_df <- nomis_get_data_util(query)

names(first_df) <- toupper(names(first_df))

if (nrow(first_df) == 0) {
stop("The API request did not return any results.
Please check your parameters.", call. = FALSE)
}

if (as.numeric(first_df$RECORD_COUNT)[1] >= max_length) {
# if amount available is over the limit of 15 total calls at a time
Expand Down Expand Up @@ -333,11 +326,10 @@ nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
if (!is.null(select) & !("RECORD_COUNT" %in% toupper(select))) {
df$RECORD_COUNT <- NULL
}

if (tidy == TRUE) {
df <- nomis_tidy(df, tidy_style)
}


df
}
12 changes: 9 additions & 3 deletions R/utils-get-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ nomis_get_data_util <- function(query) {
df <- tryCatch({
readr::read_csv(
api_get$url,
col_types = readr::cols(.default = "c",
OBS_VALUE = "d")
col_types = readr::cols(
.default = "c",
OBS_VALUE = "d"
)
)
}, error = function(cond) {
},
error = function(cond) {
message(
"It is likely that you have been automatically rate limited ",
"by the Nomis API.\n",
Expand All @@ -35,6 +38,9 @@ nomis_get_data_util <- function(query) {
)

return(NA)
}, warning = function(cond) {
stop("The API request did not return any results.\n",
"Please check your parameters.", call. = FALSE)
})

df
Expand Down
10 changes: 5 additions & 5 deletions R/utils-tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
nomis_tidy <- function(df, tidy_style) {
if (nrow(df) > 0) {
names(df) <- tolower(names(df))

if (tidy_style == "camelCase") {
names(df) <- gsub("(^|[^[:alnum:]])([[:alnum:]])", "\\U\\2",
names(df),
perl = TRUE
names(df),
perl = TRUE
)

substr(names(df), 1, 1) <- tolower(substr(names(df), 1, 1))
} else if (tidy_style == "period.case") {
names(df) <- gsub("_", "\\.", names(df), perl = TRUE)
}
}

df
}
4 changes: 1 addition & 3 deletions tests/testthat/test-codelist.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
context("test-codelist.R")

test_that("nomis_codelist is working", {

codelist <- nomis_codelist("NM_1_1", "SEX")
expect_length(codelist, 3)
expect_type(codelist, "list")
expect_true(tibble::is_tibble(codelist))
expect_equal(nrow(codelist), 3)
expect_equal(names(codelist), c("id","parentCode", "label.en"))

expect_equal(names(codelist), c("id", "parentCode", "label.en"))
})
45 changes: 30 additions & 15 deletions tests/testthat/test_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ test_that("nomis_get_data return expected format", {
expect_error(nomis_get_data(), "Dataset ID must be specified")

expect_message(
b <- nomis_get_data(
id = "NM_168_1", time = "latest",
geography = "2013265925", sex = "0", additional_queries = ""
), "The `additional_query` parameter is
deprecated, please use ... instead")
b <- nomis_get_data(
id = "NM_168_1", time = "latest",
geography = "2013265925", sex = "0", additional_queries = ""
), "The `additional_query` parameter is
deprecated, please use ... instead"
)

expect_true(nrow(b) == b$RECORD_COUNT[1])
expect_length(b, 40)
Expand Down Expand Up @@ -70,17 +71,31 @@ test_that("nomis_get_data return expected format", {
expect_type(x_select, "list")
expect_true(tibble::is_tibble(x_select))

mort_data1 <- nomis_get_data(id = "NM_161_1", date = "2016",
geography = "TYPE464",
CAUSE_OF_DEATH = "10381",
sex = 0, age = 0, MEASURE = 6)

mort_data2 <- nomis_get_data(id = "NM_161_1", date = "2016",
geography = "TYPE464", sex = 0,
cause_of_death = "10381",
age = 0, measure = 6)

mort_data1 <- nomis_get_data(
id = "NM_161_1", date = "2016",
geography = "TYPE464",
CAUSE_OF_DEATH = "10381",
sex = 0, age = 0, MEASURE = 6
)

mort_data2 <- nomis_get_data(
id = "NM_161_1", date = "2016",
geography = "TYPE464", sex = 0,
cause_of_death = "10381",
age = 0, measure = 6
)

expect_true(all.equal(mort_data2, mort_data1))
expect_true(is.numeric(mort_data2$obs_value))

expect_error(
mort_data3 <- nomis_get_data(
id = "NM_161_1", date = "2016",
geography = "TYPE46", sex = 0,
cause_of_death = "10381",
age = 0, measure = 6
),
"The API request did not return any results.\nPlease check your parameters.")


})

0 comments on commit ff994b3

Please sign in to comment.