Skip to content

Commit

Permalink
minor code tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
evanodell committed Jun 18, 2018
1 parent a8e782f commit 05a856d
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 77 deletions.
48 changes: 24 additions & 24 deletions R/codelist.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@

#' Nomis codelists
#'
#' Nomis uses its own internal coding for query concepts. \code{nomis_codelist}
#' returns the codes for a given concept in a \code{tibble}, given a dataset
#' ID and a concept name.
#' Nomis uses its own internal coding for query concepts. \code{nomis_codelist}
#' returns the codes for a given concept in a \code{tibble}, given a dataset
#' ID and a concept name.
#' Note that some codelists, particularly geography, can be very large.
#'
#'
#' @param id A string with the ID of the particular dataset. Must be specified.
#'
#'
#' @param concept A string with the variable concept to return options for. If
#' left empty, returns all the variables for the dataset specified by \code{id}.
#' Codes are not case sensitive and must be specified.
#'
#' @param search Search for codes that contain a given string. The wildcard
#' character \code{*} can be added to the beginning and/or end of each
#' search string. Search strings are not case sensitive.
#' Defaults to \code{NULL}. Note that the search function is not very powerful
#'
#' @param search Search for codes that contain a given string. The wildcard
#' character \code{*} can be added to the beginning and/or end of each
#' search string. Search strings are not case sensitive.
#' Defaults to \code{NULL}. Note that the search function is not very powerful
#' for some datasets.
#'
#' @return A tibble with the codes used to query specific concepts.
Expand All @@ -27,32 +27,32 @@
#'
#' @examples \donttest{
#' x <- nomis_codelist("NM_1_1", "item")
#'
#'
#'
#'
#' # Searching for codes ending with "london"
#' y <- nomis_codelist("NM_1_1", "geography", search = "*london")
#'
#'
#'
#'
#' z <- nomis_codelist("NM_161_1", "cause_of_death")
#' }

nomis_codelist <- function(id, concept, search = NULL) {
if (missing(id)) {
stop("id must be specified", call. = FALSE)
}

id_query <- paste0(gsub("NM", "CL", id), "_")

search_query <- ifelse(is.null(search), "",
paste0("?search=", search)
)

code_query <- paste0(codelist_url, id_query, concept,
".def.sdmx.xml", search_query)

paste0("?search=", search)
)

code_query <- paste0(
codelist_url, id_query, concept,
".def.sdmx.xml", search_query
)

df <- as.data.frame(rsdmx::readSDMX(code_query))

df

}

2 changes: 1 addition & 1 deletion R/content_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' "geoglevel", "2001census" and "sources".
#'
#' @param content_type A string with the content type to return metadata on.
#'
#'
#' @param id A string with an optional \code{content_type} id.
#'
#' @return A tibble with metadata on a given content type.
Expand Down
50 changes: 25 additions & 25 deletions R/data_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
#' columns.
#'
#' @description To find the code options for a given dataset, use
#' \code{\link{nomis_get_metadata}} for specific codes, and
#' \code{\link{nomis_get_metadata}} for specific codes, and
#' \code{\link{nomis_codelist}} for code values.
#'
#' @description This can be a slow process if querying significant amounts of
#' 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. In interactive sessions, \code{nomisr} will warn you if
#' guest users are requesting more than 350,000 rows of data, and if
#' a single tibble. In interactive sessions, \code{nomisr} will warn you if
#' guest users are requesting more than 350,000 rows of data, and if
#' registered users are requesting more than 1,500,000 rows.
#'
#' @description Note the difference between the \code{time} and \code{date}
Expand Down Expand Up @@ -80,9 +80,9 @@
#' If \code{NULL}, returns data for all available statistical measures subject
#' to other parameters. Defaults to \code{NULL}.
#'
#' @param sex The code for sexes/genders to include in the dataset.
#' Accepts a string or number, or a vector of strings or numbers.
#' \code{nomisr} automatically voids any queries for sex if it is not an
#' @param sex The code for sexes/genders to include in the dataset.
#' Accepts a string or number, or a vector of strings or numbers.
#' \code{nomisr} automatically voids any queries for sex if it is not an
#' available code in the requested dataset.
#'
#' There are two different codings used for sex, depending on the dataset. For
Expand All @@ -91,13 +91,13 @@
#' Defaults to \code{NULL}, equivalent to \code{c(5,6,7)} for datasets where
#' sex is an option. For datasets using \code{"C_SEX"}, \code{0} will return
#' results for males and females, \code{1} only males and
#' \code{2} only females. Some datasets use \code{"GENDER"} with the same
#' values as \code{"SEX"}, which works with both \code{sex} and
#' \code{2} only females. Some datasets use \code{"GENDER"} with the same
#' values as \code{"SEX"}, which works with both \code{sex} and
#' \code{gender = <code>} as a dot parameter.
#'
#' @param additional_queries Any other additional queries to pass to the API.
#' See \url{https://www.nomisweb.co.uk/api/v01/help} for instructions on
#' query structure. Defaults to \code{NULL}. Deprecated in package
#' query structure. Defaults to \code{NULL}. Deprecated in package
#' versions greater than 0.2.0 and will eventually be removed in a
#' future version.
#'
Expand All @@ -106,14 +106,14 @@
#'
#' @param select A character vector of one or more variables to select,
#' excluding all others. \code{select} is not case sensitive.
#'
#' @param ... Use to pass any other parameters to the API. Useful for passing
#'
#' @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
#' concepts identified in \code{\link{nomis_get_metadata}} and concept values
#' identified in \code{\link{nomis_codelist}}. Parameters can be quoted or
#' unquoted, and are not case sensitive. Each parameter should have a name and
#' a value. For example \code{cause_of_death = 10300} and
#' \code{CAUSE_OF_DEATH = 10300} will return the same result when querying
#' a value. For example \code{cause_of_death = 10300} and
#' \code{CAUSE_OF_DEATH = 10300} will return the same result when querying
#' dataset "NM_161_1".
#'
#' @return A tibble containing the selected dataset.
Expand Down Expand Up @@ -146,24 +146,24 @@
#' "C_OCCPUK11H_0_NAME", "obs_vAlUE"))
#'
#' tibble::glimpse(emp_by_occupation)
#'
#'
#' # Deaths in 2016 and 2015 by three specified causes, g
#' death <- nomis_get_data("NM_161_1", date = c("2016","2015"),
#' geography = "TYPE480",
#' death <- nomis_get_data("NM_161_1", date = c("2016","2015"),
#' geography = "TYPE480",
#' cause_of_death=c(10300, 102088, 270))
#'
#'
#' tibble::glimpse(death)
#'
#'
#'
#'
#' # All causes of death in London in 2016
#' london_death <- nomis_get_data("NM_1_1", date = c("2016"),
#' geography = "2013265927", sex = 1, age = 0)
#' london_death <- nomis_get_data("NM_1_1", date = c("2016"),
#' geography = "2013265927", sex = 1, age = 0)
#'
#' tibble::glimpse(london_death)
#' }

nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
sex = NULL, measures = NULL,
sex = NULL, measures = NULL,
additional_queries = NULL, exclude_missing = FALSE,
select = NULL, ...) {
if (missing(id)) {
Expand Down Expand Up @@ -212,7 +212,7 @@ nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
sex_query <- paste0("&c_sex=", paste0(sex, collapse = ","))
} else if ("SEX" %in% sex_lookup) {
sex_query <- paste0("&sex=", paste0(sex, collapse = ","))
} else if ("GENDER" %in% sex_lookup) {
} else if ("GENDER" %in% sex_lookup) {
sex_query <- paste0("&gender=", paste0(sex, collapse = ","))
} else {
sex_query <- ""
Expand Down Expand Up @@ -240,7 +240,7 @@ nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
dots <- rlang::list2(...) ## eval the dots

x <- c()

for (i in seq_along(dots)) { # retrieve the dots
x[i] <- ifelse(length(dots[[i]]) > 0,
paste0(
Expand Down
30 changes: 15 additions & 15 deletions R/metadata.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#' Nomis metadata concepts and types
#'
#' @description Retrieve all concept code options of all Nomis datasets,
#' @description Retrieve all concept code options of all Nomis datasets,
#' concept code options for a given dataset, or the all the options for a given
#' concept variable from a particular dataset. Specifying \code{concept} will
#' return all the options for a given variable in a particular dataset.
Expand All @@ -26,16 +26,16 @@
#'
#' @param additional_queries Any other additional queries to pass to the API.
#' See \url{https://www.nomisweb.co.uk/api/v01/help} for instructions on
#' query structure. Defaults to \code{NULL}. Deprecated in package
#' query structure. Defaults to \code{NULL}. Deprecated in package
#' versions greater than 0.2.0 and will eventually be removed.
#'
#'
#' @param ... Use to pass any other parameters to the API.
#'
#' @seealso \code{\link{nomis_data_info}}
#' @seealso \code{\link{nomis_get_data}}
#' @seealso \code{\link{nomis_overview}}
#'
#' @return A tibble with metadata options for queries using
#' @return A tibble with metadata options for queries using
#' \code{\link{nomis_get_data}}.
#' @export
#'
Expand Down Expand Up @@ -73,11 +73,11 @@ nomis_get_metadata <- function(id, concept = NULL,
if (missing(id)) {
stop("The dataset ID must be specified.", call. = FALSE)
}

# Warning message for additional queries
if (length(additional_queries) > 0) {
additional_query <- additional_queries

message("The `additional_query` parameter is
deprecated, please use ... instead")
} else {
Expand All @@ -101,21 +101,21 @@ nomis_get_metadata <- function(id, concept = NULL,
paste0(search, collapse = ",")
)
)

dots <- rlang::list2(...) ## eval the dots

dots_list <- c()

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

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

df <- tibble::as.tibble(rsdmx::readSDMX(
Expand Down
8 changes: 4 additions & 4 deletions R/nomisr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#' statistics and other economic and demographic data from the Office for
#' National Statistics.
#'
#' The package provides functions to find what data is available, metadata,
#' including the variables and query options for different datasets and
#' The package provides functions to find what data is available, metadata,
#' including the variables and query options for different datasets and
#' a function for downloading data.
#'
#' The full API documentation and optional registration for an API key is
#' available at \url{https://www.nomisweb.co.uk/api/v01/help}.
#' The full API documentation and optional registration for an API key is
#' available at \url{https://www.nomisweb.co.uk/api/v01/help}.
#'
#' @docType package
#' @name nomisr
Expand Down
4 changes: 2 additions & 2 deletions R/search.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' Search Nomis datasets
#'
#' A function to search for datasets on given topics. In the case of multiple
#' search parameters, returns metadata on all datasets matching one or more
#' of the parameters. The wildcard character \code{*} can be added to the
#' search parameters, returns metadata on all datasets matching one or more
#' of the parameters. The wildcard character \code{*} can be added to the
#' beginning and/or end of each search string.
#'
#' @param name A string or character vector of strings to search for in
Expand Down
10 changes: 5 additions & 5 deletions R/utils-get-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

nomis_get_data_util <- function(query) {
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(
Expand All @@ -19,7 +19,7 @@ nomis_get_data_util <- function(query) {
call. = FALSE
)
}

df <- tryCatch({
readr::read_csv(
api_get$url,
Expand All @@ -32,9 +32,9 @@ nomis_get_data_util <- function(query) {
"You can make smaller data requests, or try again later.\n\n",
"Here's the original error message:\n", cond
)

return(NA)
})

df
}
2 changes: 1 addition & 1 deletion tests/testthat/test_get_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test_that("nomis_get_metadata return expected format", {

e <- nomis_get_metadata(
"NM_1_1", "item",
geography=1879048226,sex=5
geography = 1879048226, sex = 5
)
expect_length(e, 3)
expect_equal(nrow(e), 5)
Expand Down

0 comments on commit 05a856d

Please sign in to comment.