Skip to content

Commit

Permalink
added new tests, expanded nomis_get_data functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
evanodell committed Jan 23, 2018
1 parent e9769f2 commit 5a66991
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 52 deletions.
9 changes: 4 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: nomisr
Type: Package
Title: Access nomis UK labour market data on with R
Version: 0.0.0.9001
Date: 2018-01-21
Version: 0.0.1
Date: 2018-01-23
Authors@R: person("Evan Odell", email="evanodell91@gmail.com",
role=c("aut", "cre"),
comment = c(ORCID='0000-0003-1845-808X'))
Expand All @@ -14,9 +14,8 @@ License: MIT + file LICENSE
Imports:
jsonlite,
tibble,
rjstat,
dplyr,
curl
readr,
dplyr
RoxygenNote: 6.0.1
Suggests:
knitr,
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export(nomis_get_data)
export(nomis_search)
import(dplyr)
import(jsonlite)
import(rjstat)
import(readr)
import(tibble)
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# nomisr 0.0.1

1st release of project. Rudimentary functions for retrieving information on
available datasets and downloading complete datasets from nomis.
1st release. Rudimentary functions for retrieving information on available
datasets and downloading datasets from nomis, with some limited parameters.

31 changes: 26 additions & 5 deletions R/data_download.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@


#' Retrieve nomis datasets
#'
#' Retrieves specific datasets from nomis, based on their ID. To find dataset
#' IDs, use \code{\link{nomis_data_info}}. Datasets are retrived in csv format
#' and parsed with the \code{read_csv} function from the \code{readr} package.
#'
#' @param id The ID of the dataset to retrieve.
#' @param time Parameter for selecting common dates. Accepts one of
#' \code{NULL} (returns all data), \code{"latest"} (returns the latest
#' available data), \code{"previous"} ( the date prior to "latest"),
#' \code{"prevyear"} (the date one year prior to "latest") or \code{"first"}
#' (the oldest available data for this dataset).
#' @param exclude_missing If \code{TRUE}, excludes all missing values.
#' Defaults to \code{FALSE}.
#'
#' @return A data frame
#' @return A tibble containing the selected dataset.
#' @export
#'
#' @examples \dontrun{
#'
#' x <- nomis_get_data()
#' x <- nomis_get_data(id="NM_1_1")
#'
#' y <- nomis_get_data(id="NM_1_1", time="latest")
#'
#' }
nomis_get_data <- function(id){
nomis_get_data <- function(id, time=NULL, exclude_missing=FALSE){

if(missing(id)) stop("Dataset ID must be specified")

query <- paste0("/",id,".jsonstat.json?")

time_query <- dplyr::if_else(is.null(time)==TRUE,
"",
paste0("&time=", tolower(time)))

exclude_query <- dplyr::if_else(exclude_missing==TRUE,
"ExcludeMissingValues=true",
"")

query <- paste0("/",id,".data.csv?", time_query, exclude_query)

df <- nomis_collect_util(query)

Expand Down
8 changes: 6 additions & 2 deletions R/data_sets.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
#' Retrieve all available data sets or the information available in a specific
#' dataset based on its ID.
#'
#' @param id Dataset ID. If empty, returns data on all available datasets.
#' @param id Dataset ID. If empty, returns data on all available datasets.
#' If the ID of a dataset, returns metadata for that particular dataset.
#'
#' @return A tibble
#' @return A tibble with all available datasets and their metadata.
#' @export
#'
#' @examples \dontrun{
#'
#' x <- nomis_data_info()
#'
#' }

nomis_data_info <- function(id){

if(missing(id)){
Expand Down
5 changes: 1 addition & 4 deletions R/nomisr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
#' Full API documentation available at
#' \url{https://www.nomisweb.co.uk/api/v01/help}
#'
#' The \code{nomisr} package uses the \code{rjstat} package to convert API responses to
#' data frames. As a consequence, some metadata is not included. To access
#' metadata, use the \code{nomis_data_info()} function.
#'
#' @docType package
#' @name nmisr
#' @import jsonlite
#' @import tibble
#' @import rjstat
#' @import readr
#' @import dplyr
# @useDynLib nomisr
NULL
10 changes: 6 additions & 4 deletions R/search.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
#'
#' @param search A string to search for in the name and description of
#' datasets.
#' @param keywords If \code{TRUE}, searches in dataset keywords. Defaults to
#' \code{FALSE}.
#' @param keywords If \code{TRUE}, searches in dataset keywords instead of
#' names and descriptions. Defaults to \code{FALSE}.
#'
#' @return A tibble with the search results.
#' @return A tibble with details on all datasets matching the search query.
#' @export
#'
#' @examples \dontrun{
#'
#' x <- nomis_search("")
#' x <- nomis_search("seekers")
#'
#' y <- nomis_search("Claimants", keywords=TRUE)
#'
#' }

Expand Down
8 changes: 2 additions & 6 deletions R/utils-collect.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@


## utility function to download data - may need to expand as time goes on

# the basic url for all data downloads
## utility function to download data


nomis_collect_util <- function(query){

#q <- jsonlite::fromJSON(paste0(collect_root, query), flatten = TRUE)

df <- tibble::as_tibble(rjstat::fromJSONstat(paste0("https://www.nomisweb.co.uk/api/v01/dataset", query), use_factors = T))
df <- readr::read_csv(paste0("https://www.nomisweb.co.uk/api/v01/dataset", query))

df

Expand Down
4 changes: 1 addition & 3 deletions R/utils-query.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

## utility function to query - may need to expand as time goes on

# the basic url for all data queries
## utility function for queries on available data


nomis_query_util <- function(query){
Expand Down
4 changes: 0 additions & 4 deletions R/utils-webroots.R

This file was deleted.

5 changes: 0 additions & 5 deletions man/nmisr.Rd

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

7 changes: 5 additions & 2 deletions man/nomis_data_info.Rd

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

21 changes: 17 additions & 4 deletions man/nomis_get_data.Rd

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

10 changes: 6 additions & 4 deletions man/nomis_search.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test_data_collect.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
library(nomisr)
context("nomis_get_data")


test_that("nomis_get_data return expected format", {

x <- nomis_get_data(id="NM_1_1", time="latest")

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

})
4 changes: 3 additions & 1 deletion tests/testthat/test_data_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ test_that("nomis_data_info return expected format", {
x <- nomis_data_info()

expect_length(x, 14)

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

})

0 comments on commit 5a66991

Please sign in to comment.