From 05a856d4b2f38023ee0af0f635d7899d7729b774 Mon Sep 17 00:00:00 2001 From: Evan Odell Date: Mon, 18 Jun 2018 10:16:54 +0100 Subject: [PATCH] minor code tidying --- R/codelist.R | 48 ++++++++++++++-------------- R/content_type.R | 2 +- R/data_download.R | 50 +++++++++++++++--------------- R/metadata.R | 30 +++++++++--------- R/nomisr-package.R | 8 ++--- R/search.R | 4 +-- R/utils-get-data.R | 10 +++--- tests/testthat/test_get_metadata.R | 2 +- 8 files changed, 77 insertions(+), 77 deletions(-) diff --git a/R/codelist.R b/R/codelist.R index 09b9985..eaac050 100644 --- a/R/codelist.R +++ b/R/codelist.R @@ -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. @@ -27,12 +27,12 @@ #' #' @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") #' } @@ -40,19 +40,19 @@ 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 - } - diff --git a/R/content_type.R b/R/content_type.R index 6012f5a..7ed5899 100644 --- a/R/content_type.R +++ b/R/content_type.R @@ -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. diff --git a/R/data_download.R b/R/data_download.R index 9c825d7..39311c4 100644 --- a/R/data_download.R +++ b/R/data_download.R @@ -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} @@ -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 @@ -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 = } 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. #' @@ -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. @@ -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)) { @@ -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 <- "" @@ -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( diff --git a/R/metadata.R b/R/metadata.R index 08242d1..cbbda21 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -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. @@ -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 #' @@ -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 { @@ -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( diff --git a/R/nomisr-package.R b/R/nomisr-package.R index d0c93b1..7644de3 100644 --- a/R/nomisr-package.R +++ b/R/nomisr-package.R @@ -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 diff --git a/R/search.R b/R/search.R index bbdc5cd..58a3e62 100644 --- a/R/search.R +++ b/R/search.R @@ -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 diff --git a/R/utils-get-data.R b/R/utils-get-data.R index 94dc7a8..8f47096 100644 --- a/R/utils-get-data.R +++ b/R/utils-get-data.R @@ -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( @@ -19,7 +19,7 @@ nomis_get_data_util <- function(query) { call. = FALSE ) } - + df <- tryCatch({ readr::read_csv( api_get$url, @@ -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 } diff --git a/tests/testthat/test_get_metadata.R b/tests/testthat/test_get_metadata.R index d8924af..77774a9 100644 --- a/tests/testthat/test_get_metadata.R +++ b/tests/testthat/test_get_metadata.R @@ -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)