Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
evanodell committed Apr 18, 2018
1 parent d921c2c commit 8033cee
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 95 deletions.
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ Authors@R: c(
"Evan", "Odell", email = "[email protected]", role = c("aut", "cre"),
comment = c(ORCID='0000-0003-1845-808X')),
person(
"Paul", "Egeler", email = "[email protected]", role = c("rev", "ctb"),
comment = "Reviewed package for rOpenSci: ropensci/onboarding#190"))
"Paul", "Egeler", email = "[email protected]", role = c("rev", "ctb")),
person(
"Christophe", "Dervieux", role = c("rev"),
comment = c(ORCID='0000-0003-4474-2498'))
)
Description: Access UK official statistics from the 'Nomis' database.
'Nomis' includes data from the Census, the Labour Force Survey, DWP benefit
statistics and other economic and demographic data from the Office for
Expand Down
13 changes: 11 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@


# nomisr 0.2.0

* Improved API key handling (#5)

* Increased test coverage

* Adding rOpenSci reviewers to DESCRIPTION file.


# nomisr 0.1.0

* Moved to ropensci
* Moved to rOpenSci github repository

* Added API key functionality, which is not required by the API but is
useful for large requests.
Expand All @@ -14,7 +24,6 @@ calling more than 15 pages of data at a time.
* Introduction of additional parameters to the `nomis_get_data()` and
`nomis_codes()` functions, improvements to documentation.


# nomisr 0.0.1

* 1st release. Rudimentary functions for retrieving information on available
Expand Down
24 changes: 11 additions & 13 deletions R/api-key.R
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@

#' Nomis API Key
#'
#'
#' @description Assign or reassign API key for Nomis.
#'
#'
#' @details The Nomis API has an optional key. Using the key means that 100,000
#' rows can be returned per call, which can speed up larger data requests and
#' reduce the chances of being rate limited or having requests timing out.
#'
#'
#' @details By default, \code{nomisr} will look for the environment variable
#' \code{NOMIS_API_KEY} when the package is loaded. If found, the API key will
#' be stored in the session option \code{nomisr.API.key}. If you would like to
#' reload the API key or would like to manually enter one in, this function
#' may be used.
#'
#'
#' @details You can sign up for an API key
#' \href{https://www.nomisweb.co.uk/myaccount/userjoin.asp}{here}.
#'
#'
#' @param check_env If TRUE, will check the environment variable
#' \code{NOMIS_API_KEY} first before asking for user input.
#'
#' @export
nomis_api_key <- function(check_env = FALSE) {

if (check_env) {
key <- Sys.getenv('NOMIS_API_KEY')
key <- Sys.getenv("NOMIS_API_KEY")
if (key != "") {
message("Updating NOMIS_API_KEY environment variable...")
options("nomisr.API.key" = key)
return(invisible())
} else {
warning("Couldn't find environment variable 'NOMIS_API_KEY'")
warning("Couldn't find environment variable 'NOMIS_API_KEY'")
}
}

if (interactive()) {
key <- readline("Please enter your API key and press enter: ")
} else {
cat("Please enter your API key and press enter: ")
key <- readLines(con = "stdin", n = 1)
}

if (identical(key, "")) {
stop("Nomis API key entry failed", call. = FALSE)
}

message("Updating NOMIS_API_KEY environment variable...")
options("nomisr.API.key" = key)
invisible()

}
}
42 changes: 19 additions & 23 deletions R/data_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' data. Guest users are limited to 25,000 rows per query, although
#' \code{nomisr} identifies queries that will return more than 25,000 rows,
#' sending individual queries and combining the results of those queries into
#' a single tibble.
#' a single tibble.
# In interactive sessions, \code{nomisr} will warn you if
# requesting more than 350,000 rows of data. [not currently implemented]
#'
Expand Down Expand Up @@ -203,21 +203,17 @@ nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
),
""
)

if(!is.null(getOption("nomisr.API.key"))) {


if (!is.null(getOption("nomisr.API.key"))) {
api_query <- paste0("&uid=", getOption("nomisr.API.key"))
max_length <- 100000

} else {

api_query <- ""
max_length <- 25000

}

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

Expand All @@ -227,26 +223,26 @@ nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
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
# downloads the extra data and binds it all together in a tibble

if (interactive() &&
as.numeric(first_df$RECORD_COUNT)[1] >= (15 * max_length)) {
# For more than 15 total requests at one time.
message("Warning: You are trying to acess more than ",
paste0((15 * max_length)), " rows of data.")
message("This may cause timeout and/or automatic rate limiting.")

if (utils::menu(c("Yes", "No"),
title = "Do you want to continue?") == 2) {

stop(call. = FALSE)

}
if (interactive() &&
as.numeric(first_df$RECORD_COUNT)[1] >= (15 * max_length)) {
# For more than 15 total requests at one time.
message(
"Warning: You are trying to acess more than ",
paste0((15 * max_length)), " rows of data."
)
message("This may cause timeout and/or automatic rate limiting.")

}
if (utils::menu(c("Yes", "No"),
title = "Do you want to continue?"
) == 2) {
stop(call. = FALSE)
}
}

record_count <- first_df$RECORD_COUNT[1]

Expand Down
5 changes: 2 additions & 3 deletions R/nomisr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ NULL

# Checking for API key on package load
.onLoad <- function(libname, pkgname) {

if (is.null(getOption("nomisr.API.key"))) {
key <- Sys.getenv('NOMIS_API_KEY')
key <- Sys.getenv("NOMIS_API_KEY")
if (key != "") options("nomisr.API.key" = key)
}

invisible()
}
2 changes: 1 addition & 1 deletion R/overview.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#' @examples \donttest{
#'
#' library(dplyr)
#'
#'
#' q <- nomis_overview("NM_1650_1")
#'
#' q %>% tidyr::unnest(name) %>% glimpse()
Expand Down
45 changes: 22 additions & 23 deletions R/utils-get-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,38 @@ nomis_get_data_util <- function(query) {
### Implement tryCatch
# show_condition <- function(code) {
# tryCatch(

api_get <- httr::GET(paste0(base_url, query))

if (httr::http_type(api_get) != "text/csv") {
stop("Nomis API did not return data in required CSV format", call. = FALSE)
}

if (httr::http_error(api_get)) {
stop(
paste0("Nomis API request failed with status ",
httr::status_code(api_get)
paste0(
"Nomis API request failed with status ",
httr::status_code(api_get)
),
call. = FALSE
)
}

df <- tryCatch(
{
readr::read_csv(
api_get$url,
col_types = readr::cols(.default = readr::col_character())
)
}, error = function(cond) {
message(
"It is likely that you have been automatically rate limited ",
"by the Nomis API.\n",
"You can make smaller data requests, or try again later.\n\n",
"Here's the original error message:\n", cond
)

return(NA)
}
)

df <- tryCatch({
readr::read_csv(
api_get$url,
col_types = readr::cols(.default = readr::col_character())
)
}, error = function(cond) {
message(
"It is likely that you have been automatically rate limited ",
"by the Nomis API.\n",
"You can make smaller data requests, or try again later.\n\n",
"Here's the original error message:\n", cond
)

return(NA)
})

df
}
8 changes: 4 additions & 4 deletions R/utils-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@


nomis_query_util <- function(query) {

api_resp <- httr::GET(paste0(base_url, query))
if (http_type(api_resp) != "application/json") {
stop("Nomis API did not return data in required json format", call. = FALSE)
}

if (httr::http_error(api_resp)) {
stop(
paste0("Nomis API request failed with status ",
httr::status_code(api_resp)
),
paste0(
"Nomis API request failed with status ",
httr::status_code(api_resp)
),
call. = FALSE
)
}
Expand Down
11 changes: 7 additions & 4 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

## Release summary

This is a resubmission of the `nomisr` package, with new version 0.2.0. This version removes redundant description text and places 'Nomis' in single quotes in the DESCRIPTION file.
`nomisr` provides functions to download UK census data and other official
statistics from the nomis API.
This is a resubmission of the `nomisr` package, with new version number 0.2.0.
This version removes redundant description text and places 'Nomis' in
single quotes in the DESCRIPTION file, and includes details on external
package reviewers. It also includes a more flexible method for API key
handling. `nomisr` provides functions to download UK census data and other
official statistics from the 'nomis' API.

## Test environments
* local OS X install, R 3.4.4
Expand All @@ -12,7 +15,7 @@ statistics from the nomis API.

## R CMD check results

0 errors | 0 warnings | 2 note
0 errors | 0 warnings | 2 notes

* This is a new release.

Expand Down
2 changes: 1 addition & 1 deletion man/nomis_api_key.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-load.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
context("test-load.R")

test_that("package loading works", {

expect_silent(library(nomisr))

})
2 changes: 1 addition & 1 deletion tests/testthat/test_content_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ context("nomis_content_type")

test_that("nomis_content_type return expected format", {
skip_on_cran()

expect_error(nomis_content_type())

content <- nomis_content_type("sources")
Expand Down
17 changes: 10 additions & 7 deletions tests/testthat/test_data_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ context("nomis_data_info")

test_that("nomis_data_info return expected format", {
skip_on_cran()

x <- nomis_data_info()

expect_length(x, 14)
expect_type(x, "list")
expect_true(tibble::is_tibble(x))

expect_error(nomis_data_info("lalala"),
"Nomis API did not return data in required json format")

expect_error(
nomis_data_info("lalala"),
"Nomis API did not return data in required json format"
)

y <- nomis_data_info("NM_1658_1")
expect_length(y, 12)
expect_type(y, "list")
expect_true(tibble::is_tibble(y))
expect_equal(nrow(y), 1)
expect_equal(y$id, "NM_1658_1")
expect_true(y$uri %in% x$uri)

expect_error(nomis_data_info("NM_1_6"),
"API request did not return any results")

expect_error(
nomis_data_info("NM_1_6"),
"API request did not return any results"
)
})
Loading

0 comments on commit 8033cee

Please sign in to comment.